Browsing 7239 questions and answers with Jon Skeet

Forcing C#'s HTTP Response to Return a Status Code Instead of a Description

I am currently using this script to get HTTP response headers. public static List<string> GetHttpResponseHeaders(string url) { List<string> headers = new...
Jon Skeet
people
quotationmark

With that said, it appears that StatusCode() doesn't actually return a "status code," and on successful requests, it only returns an OK instead of a 200. No, it returns an HttpStatusCode enum value. If you call ToString on an enum... more 2/6/2014 3:59:01 PM

people

Casting to custom type, Enumerable.Cast<T> and the as keyword

This is more a question out of curiosity than necessity and came about having had to deal with Active Directory (MS) types such as SearchResultCollection (in the...
Jon Skeet
people
quotationmark

Semantically I find this a little weird, since the extension method is called cast.. Shouldn't it be casting? It's casting each element if it needs to, within CastIterator... although using a generic cast, which won't use explicit... more 2/6/2014 3:47:56 PM

people

StreamWriter not closing correctly?

I'm having some trouble using the StreamWriter class correctly. I have about 10 objects I need to post, but only a couple at a time at most. However, after 2 posts, the third does...
Jon Skeet
people
quotationmark

I suspect it's more likely that it's to do with what you do with the response. The using statement will close the StreamWriter, and you don't even need the explicit Close call. However, you also need a using statement for the... more 2/6/2014 2:22:46 PM

people

Hashtable, Hashfunction: Difference between Value, Key, Hashvalue?

Lets imagine we have data we want to put in an Hashtable. The Hashfunction calculates an Hashvalue for each data object and put this hashvalues into a table (each value should get...
Jon Skeet
people
quotationmark

You always need to original key, to cope with hash collisions. The point of the hash code (or hash value as you're calling it) is to be able to very quickly find possible matches for the keys. The hash code is solely based on the key - the... more 2/6/2014 1:56:39 PM

people

Why don't the unmodifiable methods from Collections class, create collections with new elements?

Suppose there is this code: List<String> modifiableList = new ArrayList<String>(Arrays.asList("1","2","3")); List<String> unmodifiableList =...
Jon Skeet
people
quotationmark

Well the purpose of the methods is to create an unmodifiable view on an existing collection. That's the documented behaviour, and in many cases it's exactly what you want - it's much more efficient than copying all the data, for example...... more 2/6/2014 1:30:00 PM

people

Check XML node have attribute name with specific value Parse XML using Linq

First I apologize for creating a new question about XML parsing using Linq <?xml version="1.0" encoding="UTF-8"?> <html xmlns="http://www.w3.org/1999/xhtml"...
Jon Skeet
people
quotationmark

This is a namespace problem. You're looking for nav elements without specifying a namespace, but the default namespace is "http://www.w3.org/1999/xhtml". Additionally, you're looking for your epub attributes in the wrong way. I suggest... more 2/6/2014 12:33:25 PM

people

C# override Dictionary ContainsKey

I just can't find any proper piece of code to do what i need. Im using Dict.ContainsKey but due to the fact im always creating the Key i need to look for, i always get false for...
Jon Skeet
people
quotationmark

You don't need to override ContainsKey - you need to either override Equals and GetHashCode in someObj (which should be renamed to conform to .NET naming conventions, btw) or you need to pass an IEqualityComparer<someObj> to the... more 2/6/2014 12:23:42 PM

people

Change in string some part, but without one part where are numbers

For example I have such string: ex250-r-ninja-08-10r_ how could I change it to such string? ex250 r ninja 08-10r_ as you can see I change all - to space, but didn't change...
Jon Skeet
people
quotationmark

You can use regular expressions to only perform substitutions in certain cases. In this case, you want to perform a substitution if either side of the dash is a non-digit. That's not quite as simple as it might be, but you can use: string... more 2/6/2014 8:39:48 AM

people

Set timer on Buttons

I have created some button dynamically now I want to show all these buttons one by one . how could i use delay function . I use thread.sleep in loop . but it make delay for all...
Jon Skeet
people
quotationmark

You're currently blocking the UI thread - don't do that. Instead, I suggest you use a timer. For example: class Foo : Form { private int nextButtonToShow = 0; private Timer timer; private Button[] buttons; internal... more 2/6/2014 7:30:54 AM

people

How to change date Culture of a dateTime in DataTable or from GridView?

I have a some fields in database and two of them are as DateTime Field, which I am retrieving it via 'DataTable' in the GridView as in below code C# Code var connection = new...
Jon Skeet
people
quotationmark

A DateTime value doesn't have a culture, inherently. It's just a value. I suspect that you should change this though: DataFormatString="{0:g}" For example, change it to: DataFormatString="{0:yyyy/MM/dd HH:mm:ss}" (Note the HH rather... more 2/6/2014 7:19:17 AM

people