Browsing 7239 questions and answers with Jon Skeet

Can you include type parameters when overloading operators in c#?

I want to do something in the same spirit of this non-compiling code public static B operator + (Func<A,B> f, A a) { return f (a); } It there a way to specify the...
Jon Skeet
people
quotationmark

No, operators can't be generic in C#. You can overload operators within generic types, but the operators themselves can't have type parameters. If you look at the syntax for user-defined operators in the C# spec (e.g. in section 10.10 of... more 7/21/2014 6:18:11 AM

people

Can I assign a null value to an anonymous type property?

I have the following in WebAPI that is converted to a JSON string and sent to the client: return Ok(new { Answer = "xxx", Text = question.Text, ...
Jon Skeet
people
quotationmark

Absolutely - you just need to cast the null value to the right type so that the compiler knows which type you want for that property. For example: return Ok(new { Answer = (string) null, Text = ..., ... }); more 7/20/2014 3:22:57 PM

people

Instantiate object via reflection with anonymous type

I'm using the following method to instantiate an object via reflection Activator.CreateInstance(Type type, params object[] parameters) Where "parameters" is the list of...
Jon Skeet
people
quotationmark

I wouldn't use Activator.CreateInstance for this. I'd use Type.GetConstructors() to get all the constructors, and then find one which has the same number of parameters as the anonymous type has properties, and with the same names. If there... more 7/20/2014 3:16:20 PM

people

Match lines before empty new line

The input is like this: 0 00:00:00,000 -- 00:00:00,000 Hello world! 1 00:00:00,000 -- 00:00:00,000 Hello world! This is my new world. 2 00:00:00,000 -- 00:00:00,000 Hello...
Jon Skeet
people
quotationmark

Well, here's a non-regex approach: public IEnumerable<List<string>> ReadSeparatedLines(string file) { List<string> lines = new List<string>(); foreach (var line in File.ReadLines(file)) { if... more 7/20/2014 8:42:05 AM

people

Error in generic code

I am getting this error "Type mismatch: cannot convert from Object to E" in the last line of the pop() method. Node public class Node<E> { E item; Node next; ...
Jon Skeet
people
quotationmark

This is the problem in Stack<E>: private Node head; and likewise later: Node nodeToBePopped; And in Node<E> itself: Node next; You're using the raw type Node here, so all the generics are lost, effectively. See the... more 7/20/2014 8:26:45 AM

people

'fib' is not recognized as an internal or external command, operable program or batch file

My execution lines in CMD are D:\>cd jav D:\jav>javac fib.java D:\jav>fib java 'fib' is not recognized as an internal or external command, operable program or...
Jon Skeet
people
quotationmark

You've got the command line round the wrong way. You want: java fib or preferably follow Java naming conventions, calling your class Fib rather than fib, and use java Fib (Even better, use packages - but that's probably the next... more 7/19/2014 10:13:12 AM

people

How to ignore reference to jetbrains.annotations.dll in NodaTime nuget package xml documentation file

I lean quite heavily on Resharper context actions to generate boilerplate code. The Check parameter for null context action had, up until recently, generated code that performs...
Jon Skeet
people
quotationmark

Noda Time deliberately ships with an internal copy of the R# annotations, so that those who do want them can benefit from them - it will know which methods are pure, etc. It sounds like you don't actually want to use annotations at all,... more 7/18/2014 11:17:59 PM

people

Identify parentheses in lambda expression with ExpressionVisitor in C#

I have a library that converts a lambda expression to an odata filter string using the ExpressionVisitor class. For example, the expression o => o.Teste == null &&...
Jon Skeet
people
quotationmark

The node type for not isn't for !(expression) it's just for !expression - the parentheses aren't part of it. Parentheses are necessary in source code to indicate the precedence you want. They're not necessary in expression trees because... more 7/18/2014 7:28:22 PM

people

Are Java anonymous classes created at runtime?

Are anonymous Java classes created at runtime or ahead of time by the compiler? According to the Java docs, They are like local classes except that they do not have a name, so...
Jon Skeet
people
quotationmark

They're created by the compiler. You can see them by just compiling some code and looking at what you get on disk. You'll end up with things like Foo$1.class where Foo is the class that contains the anonymous method. For example: public... more 7/18/2014 6:33:32 AM

people

How to get my poker hand to print?

I am having some trouble getting my poker hand to print. I'm not sure if there is something wrong in the hand class or in my dealCard method in my deck class. When I try and run...
Jon Skeet
people
quotationmark

Firstly, it's important to be able to diagnose this yourself more. Either adding some logging or using a debugger should help you work out what the code is doing - you shouldn't just need to depend on perfect output. Secondly, some unit... more 7/18/2014 5:32:42 AM

people