96 points · by kristianp · 13y ago · 93 comments
Consider (stolen from the spec doc):
int[] values = { 7, 9, 13 };
Action f = null;
foreach (var value in values)
{
if (f == null) f = () => Console.WriteLine("First value: " + value);
}
f();
In previous versions of C#, there is a single value variable is in the scope of the entire loop, so the above code would actually print out the last value in the list because that was the value of value (sorry) when the loop finished. In C# 5.0, a value variable is actually in the scope of each individual iteration (so within the braces). So now if you use your iteration variable in a closure scope it won't change on you when the loop iterates and the above code actually prints out the first value.The same cannot be said for a for loop. It still functions as it always has.
http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closi...
It could always be worse, though. Look at JavaScript. Simply copying the loop variable in the body of the loop doesn't work because all variables are scoped at the function level. So your "local" loop variable is actually a single instance for the entire function and you have the same exact problem. Instead, you have to actually create another function and immediately execute it with the loop iteration variable as an argument, and then use the parameter within the function to do whatever you wanted to do. Not cool.
It really was a horrible wart that needed to be fixed.
Now if they would only fix the misleading lying dishonest "downcast on type bound" compiler error messages, then C# would lack warts all together.
I'm sure a few people were wondering if this was to go alongside Visual Studio 2013, expected to be released in beta later this week.
[1] http://msdn.microsoft.com/library/hh191443.aspx
My point being that c# 5.0 is around 1 year old, and this spec is not new.
5.48 MB / Has bookmarks from document headers / For download and offline reading if you want to navigate to specific chapter quickly / Not for online viewing (the parent version is only 3.70 MB and is better if you're bandwidth-capped).
No Anders & co didn't just add whatever they felt fashionable, they've designed a language over the course of a decade with (to my knowledge) zero warts, bringing to the table more than 20 years of prior design experience (Turbo Pascal, Delphi).
If it weren't for the stigma of being a Microsoft design, C# would be a hugely more popular language.
Still, great language, love it.
C# is a nice language, but oh man, is that asking for it!
My pet example of a wart is the set of requirements on the iterated collection in foreach loops. Most C# developers probably think that the collection has to implement IEnumerable, but it is not so! I pasted the requirements from the spec at http://pastebin.com/EfsAmz6f
This is also a case where the dynamic type does something different than the static type. If you iterate over a statically typed non-IEnumerable collection, it will work fine (assuming it implements the required methods). But if you then change the type to dynamic, I think you get a runtime error.
So there's a wart for ya!
Any idea why they did this? The only thing this seems to achieve is that you can implement IEnumerable and IEnumerator without explicitly stating it. Are there any (common) classes to which this applies?
class Heh : IEnumerable {
public void Add(object x) { Console.WriteLine(x); }
public IEnumerator GetEnumerator() { return null ; }
}
static void Main() { new Heh { "a", 1, DateTime.UtcNow }; } }
Prints out: a, 1, 6/25/2013 4:51:38 AM,
It might have been the right engineering approach all things considered. But having these special things that only the language can decide is annoying. The fewer the language primitives, the better.There was definitely one that got me (they made a breaking change to fix it in C# 5.0, though).
http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closi...
I agree completely, though, that it is remarkable how well-designed the language is.
* Garbage collection
* Lightning fast compilation
* Pointer-safe subset good enough for most common uses.
LINQ is also considered a huge win by many, though I suspect it could also be done in C++.
While I haven't used, the Xamarin iOS/Android development platforms look quite interesting. I suppose it has the stigma of a somewhat pricy per seat license but its likely worth it if you want to go with a shared codebase for iOS/Android/WP/Win8.
There's also MonoGame[1] which targets developers of MS's former XNA game dev kit.
The stuff actually fits in the language pretty well. What has resulted, however, is that there are multiple ways to skin a cat in C#, and there's very limited guidance on what's considered idiomatic.
This is complicated by the fact that many .NET/C# shops have coding standards in place that don't go much beyond C# 2.0. The last C# interview I did, at an allegedly "progressive" .NET shop, the interviewer barred me from using null coalesce in a whiteboard example because "That's hobby project code. It's not how we write production code"
Great language, terrible culture.
I too had a boss (new owner after acquisition) say "no you're not allowed to use lambdas".
This is typical enterprise stuff, I bet most typical enterprise already making the switch to Scala and F# also force their developers to type annotate everything.
I find C# to be much more idiomatic than C++ or Scala. If you think C# is bad...stay away from those languages.
The consulting company I work for still gets requests for project in Java 1.4, for example.
My last .NET project we had some issues because there was a mix of .NET 3.5 and 4.0 across teams.
As far as I know dynamic was added to ease interop with native code - I have never seen it heavily used in regular code. LINQ and lambda expression have been introduced together with the Entity Framework and I am unable to imagine using an O/R mapper without something similar again - it just fits together so nicely.
Async was an obvious next choice - we are getting more and more cores and traditional thread-based parallel programming just does not scale because it is so error prone. And after they extended LINQ to PLINQ they had a good part of the necessary infrastructure already in place.
[1] For example they would like to add something like Java's checked exceptions but when they considered it they came to the conclusion that there are to many unsolved problems and that it might require a few more years of research to come up with a really good way to do it and so they postponed it for later reconsideration. In general there is a lot of well researched theory behind the things they do. Another example is the Entity Framework. They didn't just implement another O/R mapper but came up with a mathematical theory comparable to the relational algebra and then implemented it based on that results.
LINQ's another example of a place where the C# team sighted one end advantage (queries) and implemented the minimum to make it work. That's why all the features around the main LINQ part are half-assed (limited expression trees, ambiguous lambda syntax, very limited type inference.)
Overall, C#'s a nice enough language for a lot of projects. The tooling really helps sells it, too. It's just rather verbose and limiting, unnecessarily. I just can't think of any place where C# the language is significantly better than F#.
But, if you're ranking it against the likes of Java, then yes, C# is close to perfect.
Edit: C# implements a lot of stuff right into the compiler, things that'd be useful more generically. For instance, foreach and collection initializers use duck typing that's baked into the C# compiler. That's a generally useful feature, but again, hard coded for a specific scenario. F# has some of that too, but not to the same extent (F# warts arise mainly when you push the edge of F#-C#ish interop).
Currently my F# code is mainly for scripting when doing .NET projects, but only if I am the only user of such scripts, otherwise Powershell.
Anyway I really appreciate the work Microsoft puts on F#, Haskell and OCaml via their research arm.
I'm sure I'm not the only one who misses these features when they're working in Java.
I've been a c# developer for over 5 years and I have to disagree with almost everything you said. Ruby blew my mind when I learned it.
Good to see nothing ever really changes in Redmond.
My Android doesn't have Quick Office, it does have ThinkOffice but I can think of no reason I would a) read a computer language spec on my cell phone, or b) ever use ThinkOffice on purpose.
If there is a good reason to edit, perhaps they should have made it an HTML document. Use LaTeX. Are they afraid the Office division will get them fired if they use anything other than Microsoft products?
PDF, sure. Postscript, uh, maybe missing the target audience a bit there.
> If there is a good reason to edit, perhaps they should have made it an HTML document. Use LaTeX. Are they afraid the Office division will get them fired if they use anything other than Microsoft products?
hahahahaha. Perhaps they think it's a good wordprocessing product which can be used by many different roles in the business?
And yes, there's a lot to be said for a company using its own products where they fit.
Not sure if there is anything else though.