Browsing 7239 questions and answers with Jon Skeet

Reference to an instance method of a particular object

In the following code, it works when passing the method reference variable with the class name, but when passing the reference variable with a user object there is an...
Jon Skeet
people
quotationmark

userList.forEach is expecting a Consumer<? extends User> - in other words, a method which accept a User reference and do something with it. That could be: A static method accepting a User parameter, in which case the parameter... more 8/20/2014 4:18:15 PM

people

How does XDocument.Load(Stream) detect end of XML message?

Im reading a XDocument from a TcpClient stream. While implementing, I was wondering how the XDocument.Load(Stream) knows when the document is completed? Several ideas crossed my...
Jon Skeet
people
quotationmark

It's easy to prove that it reads past the end element - you just need to create an XML file which contains data after the end element, making it invalid: <root> <child /> </root> More stuff If you load that, you'll... more 8/20/2014 1:10:48 PM

people

GZipStream not send when I want

I'm trying to use GzipStream over a socket. For this purpose I use something like this. using System.IO.Compression; TcpClient _client = null; // ... // ... Connection...
Jon Skeet
people
quotationmark

Use the overload of the GZipStream constructor which accepts a bool parameter to say whether to leave the underlying stream open afterwards: using (var zip = new GZipStream(networkStream, CompressionMode.Compress, true)) { zip.Write... more 8/20/2014 11:35:40 AM

people

Accessing the element type in XML using LINQ to XML

As seen in the code snippet below, the name given to each of my elements in the XML file vary. Element 1 is of type testcheck1 and Element 2 is of type testcheck2. <?xml...
Jon Skeet
people
quotationmark

It sounds like you're really just looking for the element name, possibly its LocalName: var root = XElement.Load("project_data.xml"); foreach (var element in root.Elements()) { Console.WriteLine("{0}: {1}", ... more 8/20/2014 10:38:28 AM

people

How to best juggle between child and parent objects

I am going to have a very large database of objects loaded into memory that contain a string (path) and a UInt (index number). For a smaller selection of these at any given time I...
Jon Skeet
people
quotationmark

Perhaps use composition instead of inheritance - have an extra property of what's currently your base class for "extended properties"... then just populate that property lazily: public class FileInfo // Or whatever { public string... more 8/20/2014 8:52:04 AM

people

Call different methods in a generic method based on the type parameter

I have a few methods like this: public string GetStringValue(string field) { /* ... */ } public int GetIntValue(string field) { /* ... */ } Now I want to write a generic method...
Jon Skeet
people
quotationmark

You can get it to work with casting, but it's ugly: if (typeof(T) == typeof(string)) { copyAction((T)(object) GetStringValue(field)); } (etc) This sort of thing always ends up being fairly ugly, to be honest. One option would be to... more 8/20/2014 8:31:22 AM

people

The type or namespace name 'T' could not be found while Implementing IComparer Interface

I am trying to implement IComparer Interface in my code public class GenericComparer : IComparer { public int Compare(T x, T y) { throw NotImplementedException; ...
Jon Skeet
people
quotationmark

Your GenericComparer isn't generic - and you're implementing the non-generic IComparer interface. So there isn't any type T... you haven't declared a type parameter T and there's no named type called T. You probably want: public class... more 8/20/2014 7:44:22 AM

people

Get properties of empty linq statement

Is there a way of looping through the properties of the underlining result of the linq statement when the linq statement contains no elements? I would like to create a DataTable...
Jon Skeet
people
quotationmark

I suspect all you're looking for it: PropertyInfo[] queryInfo = typeof(T).GetProperties(); In other words, get the properties from the generic type argument rather than for a particular instance of that type. more 8/20/2014 7:33:42 AM

people

Dictionary with 2 values

That's my CSV file Test.csv: AAA, 81, 82, *, * BBB, 83, 84, *, * CCC, -, 86, *, * DDD, 87, 88, *, * EEE, 89, 90, *, * FFF, 99, -, -, * GGG, 102, 108, -, * Normally, I use this...
Jon Skeet
people
quotationmark

I'd just create a new class or struct to encapsulate the two values. You could use Tuple<,> instead, but personally I'd create a new type so that you can use friendlier property names. You might want to include all the data from the... more 8/20/2014 6:54:06 AM

people

How can I use generics to specify the type of an out parameter in a base class' method?

I am wrapping the ExceptionManager in Microsoft's Enterprise Library's Exception Handling block, and I want to allow for consumers of my wrapper to specify which exception types...
Jon Skeet
people
quotationmark

The argument you use for an out parameter has to exactly match the declared parameter type. As HandleException has a third parameter out Exception exceptionToThrow, that won't work at the moment. Given that you don't need the exception to... more 8/19/2014 8:00:13 PM

people