
C# Language Specification 5.0 - kristianp
http://www.microsoft.com/en-US/download/details.aspx?id=7029
======
skwirl
An interesting change in C# 5.0 involves the handling of foreach iteration
variables when used in closure scope. This is technically a breaking change,
although it is really hard to think of a case where the old behavior was
legitimately desired.

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.

~~~
gnaritas
Should have been that way from the beginning; having the loop variable have
function scope was absurd to begin with and forced developers to copy the loop
var to a local before using it in a lambda.

~~~
skwirl
Yeah, they could have made the change in C# 3.0. I just don't think anyone
really felt the pain until the Task Parallel Library came along in .NET 4.
Since the TPL API is lambda heavy people really started using lambdas for
asynchronous tasks, which leads (or used to lead, anyway) to pain and misery
when using a loop iteration variable.

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.

~~~
gnaritas
Agreed.

------
samrift
A note to those that may be confused: C# 5.0 is the language version used by
visual studio 2012, which was released in August of last year, alongside the
.net framework 4.5. This is an updated revision, although a cursory look
didn't reveal any differences to me.

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.

~~~
danbruc
C# 5.0 adds asynchronous methods [1] and caller info attributes [2] to the
language. They also changed respectively fixed the way foreach loop variables
are captured in closures [3] and changed the overload resolution algorithm [3]
and argument evaluation order [3].

[1]
[http://msdn.microsoft.com/library/hh191443.aspx](http://msdn.microsoft.com/library/hh191443.aspx)

[2]
[http://msdn.microsoft.com/library/hh534540.aspx](http://msdn.microsoft.com/library/hh534540.aspx)

[3]
[http://msdn.microsoft.com/library/hh678682.aspx](http://msdn.microsoft.com/library/hh678682.aspx)

~~~
samrift
I'm sorry, I meant that I didn't see any differences from the C# 5.0
specification released in August 2012 and the revision posted here, which was
updated in June 2013. I am aware of the differences from c#4 to c#5.

My point being that c# 5.0 is around 1 year old, and this spec is not new.

~~~
danbruc
Maybe someone else will still find the information helpful. I also just
noticed that the copyright still says 1999-2012 - maybe they just reuploaded
it.

------
yjh0502
Here's PDF version:
[https://www.dropbox.com/s/dt6xzum432zine0/CSharp%20Language%...](https://www.dropbox.com/s/dt6xzum432zine0/CSharp%20Language%20Specification.pdf)

~~~
3825
Here's another version:
[https://www.dropbox.com/s/4n6sg31v0v3nfok/CSharp%20Language%...](https://www.dropbox.com/s/4n6sg31v0v3nfok/CSharp%20Language%20Specification.pdf)

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).

~~~
voltagex_
I'm curious - what's the difference between yours and the parent's in terms of
the conversion?

~~~
3825
This one is slightly bigger (a trade-off) because I included bookmarks
(created from headers in the word document).

------
kailuowang
Asynchronous method... interesting. It seems to me that each version they just
incorporate a little bit of some hot paradigms into this language that started
as a sweeter(or less painful) java. I remember that during the ruby boom they
added the "dynamic" feature into it. Together with linq, and lambda, is it me
or all these attempts sometimes appear awkward and out of place?

~~~
spamizbad
As a former C# dev, here's my subjective view:

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.

~~~
MichaelGG
Yes, the whole fights of people confused about "var" and avoiding it shows
that a fair amount of MS's target group is simply not interested in proper
language research or design. I'll admit that I love the ideas behind the CLR
(the VM design is pretty slick, although it's stagnated the past years). I
think some parts are really excellent (the generics), but when I approach a
vendor and I hear they're using .NET, it is a slightly negative flag.

I too had a boss (new owner after acquisition) say "no you're not allowed to
use lambdas".

~~~
pjmlp
If you want to start a flamewar just go to a C++ newsgroup and ask around
about using type inference with auto.

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.

------
sshconnection
I wish we had an accurate spec for Ruby. RubySpec project is great, but we
still let the language be defined in terms of MRI implementation.

~~~
seanmcdirmid
Sun started the trend when Guy Steele and Gilad Bracha (among others) wrote
the initial Java Language Specification. Microsoft has a strong team of PL
researchers, including Mads Torgersen and many people at MSRC (who published a
paper on formalizing Async in ECOOP 2012), to do this work, which I think is
very useful.

------
mpyne
MS Word or Word Viewer to see the specification? Really?

Good to see nothing ever really changes in Redmond.

~~~
dangrossman
Is there something in this document that prevents you from reading it in your
preferred viewer? The extension suggests it's just an Office Open XML doc. I
can read it in QuickOffice on my Android phone.

~~~
mpyne
Given that my preferred viewer for this kind of thing is Okular, with a
fallback to Firefox+pdf.js if necessary, it's already struck out twice.

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.

~~~
dangrossman
I think you've missed my point there. I wasn't suggesting you read it on your
phone, I was demonstrating that you don't need Word using what was handy to me
at the time (QuickOffice is owned by Google and was preinstalled). It's not a
Word document. It's an office open XML document which dozens of free and
commercial word processors and web apps can read and write. Not using your
choice of previously-proprietary document formats doesn't make this an "oh,
Redmond" moment.

~~~
JoshTriplett
Office "Open" XML _is_ a proprietary format that got rubber-stamped as a
standard to convey a sense of legitimacy. Significant parts of that standard
say "do it like Word does" with varying degrees of indirection. Other office
suites and viewers support it because they have to, not because the "standard"
makes it any easier to do so.

Comments like this one suggest that the rubber stamp does indeed convey some
legitimacy, which I'd consider unwarranted.

As a specification for a language coming out of Microsoft, it isn't
_unexpected_ to see it in Word format, and it's certainly possible to cope
with it using any number of other tools without resorting to Word, but that
doesn't make it a good idea. Use PDFs.

~~~
tzs
> Significant parts of that standard say "do it like Word does" with varying
> degrees of indirection

That's not correct. What it actually does is reserve some markup for use by
third parties that have reverse engineered various old programs (including
programs that competed with Microsoft programs), so that if those people have
workflows that depend on features of those old programs that cannot be
represented in OOXML, they can still use OOXML as a storage format but add in
the extra information they need.

Here's the use case this is aimed at. Suppose I run, say, a law office, and
we've got an internal document management system that does things like index
and cross reference documents, manage citation lists, and stuff like that. The
workflow is based on WordPerfect format (WordPerfect was for a long time the
de facto standard for lawyers).

Now suppose I want to start moving to a newer format for storage. Say I pick
ODF, and start using that for new documents, and make my tools understand it.
I'd like to convert my existing WordPerfect documents to ODF. However, there
are things in WordPerfect that cannot be reproduced exactly in ODF, and this
is a problem. If my tools need to figure out what page something is on, in
order to generate a proper citation to that thing, and I've lost some
formatting information converting to ODF, I may not get the right cite.

So what am I going to do? I'm going to add some extra, proprietary markup of
my own to ODF that lets me include my reverse engineered WordPerfect knowledge
when I convert my old documents to ODF, and my new tools will be modified to
understand this. Now my ODF workflow can generate correct cites for old
documents. Note that LibreOffice won't understand my additional markup, and
will presumably lose it if I edit a document, but that's OK. The old documents
I converted should be read-only.

Of course, I'm not the only person doing this. Suppose you also run a law
office, with a WordPerfect work flow, and are converting to an ODF work flow.
You are likely going to add some proprietary markup, just like I did. We'll
both end up embedding the same WordPerfect information in our converted legacy
documents, but we'll probably pick different markup for it. It would be nice
if we could get together, make a list of things we've reverse engineered, and
agree to use the same markup when embedding that stuff in ODF.

And that's essentially what they did in OOXML. They realized there would be
people like us with our law offices, who have reverse engineered legacy data,
that will be extending the markup. So they made a list of a bunch of things
from assorted past proprietary programs that were likely to have been reverse
engineered by various third parties, and reserved some markup for each.

------
bsaul
Anyone knows if C# is used inside microsoft to buiild desktop app ( office ?
Visual studio ? ) I remember it wasn't the case for a long time but stopped
watching since.

~~~
TheMakeA
The Visual Studio IDE and Expression Blend are (or at least were) WPF... I
guess there's a pretty good chance it was C# rather than VB.NET.

Not sure if there is anything else though.

~~~
rbirkby
Visual Studio since 2002 (.Net 1.0) has been partly written in C#/WinForms and
then C#/WPF. Visual Interdev (from which the current Visual Studio lineage
descends) was partly written in VJ++. It was these bits that were ported to
C#/COOL for the 2002 release.

------
BillyMaize
Why, if this is version 5.0, does the document it downloads say version 4.0?
Am I the only one seeing this? The details also mention previous version 3.0
on the webpage.

