Browsing 7239 questions and answers with Jon Skeet

Building generic order by statement

I have a class with a bunch of properties: class Foo { public string Name {get; set; } public int Age {get; set; } and a collection of instances of Foo. Now I want...
Jon Skeet
people
quotationmark

The problem is that you're trying to build a Func<T, int> but your call to Expression.Lambda doesn't specify the parameter expression, which means you can't expect it to create a delegate that has any parameters. Just specifying type... more 12/17/2015 10:34:58 AM

people

Why does LINQ to XML not escape characters like '\x1A'?

I get exception if in XElement's content I include characters such as '\x1A', '\x1B', '\x1C', '\x1D', '\x1E' or '\x1F'. using System; using System.Collections.Generic; using...
Jon Skeet
people
quotationmark

Most of those characters simply aren't valid in XML 1.0 at all. Personally I wish that LINQ to XML would fail to produce a document that later it wouldn't be able to parse, but basically you should avoid them. I would also recommend... more 12/17/2015 9:56:16 AM

people

Get the type parameter of a Class, by knowing only the Type of Class

I have a base abstract class having a type parameter from another abstract class, as: public abstract class Database<T> where T : DatabaseItem, new() { //... } public...
Jon Skeet
people
quotationmark

If you're looking for the type arguments for a specific class, that's relatively easy: static Type GetDatabaseTypeArgument(Type type) { for (Type current = type; current != null; current = current.BaseType) { if... more 12/17/2015 7:25:10 AM

people

c# task acting on later reference

for(int i = 0; i < list.Count(); i++) { Task task = new Task(() => DoWork(list[i])); task.Start(); } Can someone tell me how I can ensure that the DoWork function will...
Jon Skeet
people
quotationmark

Yes, the variable is captured by the lambda expression, and you're then modifying it. There are a few simple options: First, copying the variable to a variable which is instantiated within the loop: for (int i = 0; i < list.Count;... more 12/17/2015 6:58:18 AM

people

why is FileReader not a class in NetBeans?

So I just switched to NetBeans today because my eclipse had been crashing for a bizillion times. However when I am learning something on reading files, I can't seem to import the...
Jon Skeet
people
quotationmark

You simply haven't imported it - you either need a wildcard import: import java.io.*; or the specific import: import java.io.FileReader; As noted by Titus, you're also missing a new before you use FileReader.... more 12/17/2015 6:43:43 AM

people

When the static block will be executed in JAVA while creating object?

class DemoClass { public static void main(String args[]) { System.out.println("Start"); A a=new D(); } } class A { static { ...
Jon Skeet
people
quotationmark

The static initializer of all the classes have started executing - but in order to initialize D, C must be initialized, so B must be initialized, so A must be initialized. At the point where the code in the static initializer in A is being... more 12/16/2015 2:22:58 PM

people

Implementing IDisposable correctly on parent classes in C#

I have a class which implements the C# SerialPort which used to look like this: public class AsyncSerial : IDisposable { SerialPort newPort; //Parameters declared in my...
Jon Skeet
people
quotationmark

Your subclass should override Dispose(bool disposing) if anything - that's the whole point of having that method at all, really. However, I suspect that the base class will make the right calls anyway, so you shouldn't need to do... more 12/16/2015 12:01:05 PM

people

Access fields wrapped inside anonymous type or Object class

I have a method which returns as follows, return new { a, b, c, d}; And method definition is public object GetValues(); How do I access those variables a, b, c, d after...
Jon Skeet
people
quotationmark

Two options: Use dynamic typing, so long as you're using it from the same assembly: dynamic values = GetValues(); var a = values.a; //etc Use reflection directly; the generated type will have public read-only properties called a, b, c... more 12/16/2015 10:59:26 AM

people

What is wrong with this object being capable of telling how many of its properties are not null/whitespace/empty?

I am trying to figure out why the following code throws a StackOverflowException (I am finally posting a StackoverflowException in SO!). Debugging seems to point out that the...
Jon Skeet
people
quotationmark

You have a property, which, when you execute the "get" accessor, finds all the properties and fetches their value. So it executes itself, recursively. If you only want string properties, you should check the property type before fetching... more 12/16/2015 10:51:10 AM

people

Why is 'as' keyword needed to access functionality when already checked with 'is' keyword?

Recently I started making more use of interfaces and inheritance, and I try to pass those as parameters into functions to make my code more flexible. But what really annoys me...
Jon Skeet
people
quotationmark

Why can't I just use mailObject.GetAttachments() after I check if it implements the interface (or baseclass for that matter) with is? Because the compile-time type of the mailObject variable is still just T. Note that currently... more 12/16/2015 10:23:47 AM

people