News for hackers. Stuff that matters.
Re:C# Hidden Optimizations by heresy on Tuesday October 12, 2010 @01:58AM (#1782467)
Nit: These aren't "optimizations", they're syntactic sugar.
Re:C# Hidden Optimizations by achew22 on Tuesday October 12, 2010 @02:26AM (#1782510)
I'm far from impressed with this. How is ?? any different from a guard ( || )? I use that all the freaking time in every language. I've never written a line of C# and even I know about the parallel module, how didn't that make the cut?
Re:C# Hidden Optimizations by mullr on Tuesday October 12, 2010 @03:04AM (#1782577)
C# has || just like any other language, but objects aren't truthy and nulls aren't falsy. This was a good design decision, since it catches the common = vs. == error if(a=b) while doing the type check rather than silently doing the (probably) wrong thing. As a consequence, the || doesn't work as it does in other places. Hence ??.
Re:C# Hidden Optimizations by steverb on Tuesday October 12, 2010 @09:21AM (#1783224)
?? is the null coalescing operator and it's neither new nor is it hidden.

It is damned handy though.

Re:C# Hidden Optimizations by newt on Tuesday October 12, 2010 @09:24AM (#1783233)
Every language?

Java, as far as I know, does not allow if (someObject) where someObject is a class type. AFIAK, java only allows that if someObject is a bool, or possibly an int as well.

In strongly typed languages in general, null is not the same thing as false and not-null is not the same thing as true.

Re:C# Hidden Optimizations by oozcitak on Tuesday October 12, 2010 @04:43AM (#1782738)
I would say expression trees is one of the less well-known and under-appreciated features of C# 3.0:

    Expression<Func<int, int>> e = (x) => x + 1;
    Func<int, int> f = e.Compile();
    int b = f.Invoke(42);
Re:C# Hidden Optimizations by arethuza on Tuesday October 12, 2010 @05:11AM (#1782781)
I'm quite fond of C# but that syntax is a bit much when compared to things like humble old JavaScript:

   var f = function(x) { return x + 1; };
   var b = f(42);
Re:C# Hidden Optimizations by newt on Tuesday October 12, 2010 @09:10AM (#1783190)
The Expression tree part is powerful (you can inspect it with code, pull property names out, etc), but it's not needed in most cases. Your example in C# is:

  Func<int, int> f = x => x + 1;
  int b = f(42);
This is shorter than the JavaScript. The C# is more verbose where has to specify a type for f because it cannot work it out from context (in fact the left side of the '=' supplies type information to the right side), but the actual expression on the right is less verbose than the JavaScript with its "function" and "return" keywords.

If you want, the second line can be

  var b = f(42);
But it's the same number of chars. I don't think that you gain anything from letting the compiler work out that b is an int.
Re:C# Hidden Optimizations by profquail on Tuesday October 12, 2010 @07:55AM (#1783011)
The best thing about expression trees isn't that it lets you declare a method like Func<Func<int>, int> = ... (or whatever), but that you can inspect (and modify) the expression tree at runtime. This is what powers LINQ, and the 'static reflection' used by fluent APIs like Fluent nHibernate.
Re:C# Hidden Optimizations by yread on Tuesday October 12, 2010 @06:44AM (#1782912)
well you can write it as

  var f = ((x) => x + 1).Compile();
  int b = f.Invoke(42);
Re:C# Hidden Optimizations by oozcitak on Tuesday October 12, 2010 @07:15AM (#1782957)
I don't think that would work. How would the compiler know the type of x and the return type of the lambda expression?
Re:C# Hidden Optimizations by the_rara_avis on Tuesday October 12, 2010 @10:18AM (#1783441)
It won't. In just about all cases, C# doesn't do backtracking to infer types. See Eric Lippert's posts: http://blogs.msdn.com/b/ericlippert/archive/2010/10/04/no-ba...
Re:C# Hidden Optimizations by newt on Tuesday October 12, 2010 @09:16AM (#1783203)
This article is Ok, in an introductory sort of way, but these are not "optimizations" in the sense that they will make your code run faster. They're syntactic sugar as heresy says.

They're also not very hidden either.

Re:C# Hidden Optimizations by Rickasaurus on Tuesday October 12, 2010 @09:52AM (#1783346)
How did this garbage get on the HN front page?