what is new in c# 3?

D

Dan Holmes

Linq and the features necessary to support it get a lot of attention but
i happened to find out about partial methods by chance. Is there a
comprehensive list of new c# stuff somewhere? i couldn't find anything
obvious on the c# page on msdn.

dan
 
S

sherifffruitfly

Linq and the features necessary to support it get a lot of attention but
i happened to find out about partial methods by chance.  Is there a
comprehensive list of new c# stuff somewhere?  i couldn't find anything
obvious on the c# page on msdn.

dan

Whatever Jon Skeet says is new. Only 1/2 joking.
 
J

Jon Skeet [C# MVP]

Dan Holmes said:
Linq and the features necessary to support it get a lot of attention but
i happened to find out about partial methods by chance. Is there a
comprehensive list of new c# stuff somewhere? i couldn't find anything
obvious on the c# page on msdn.

Here we go:

Partial methods
Automatically implemented properties
Implicitly typed local variables
Object initializers
Collection initializers
Implicitly typed arrays
Anonymous types
Extension methods
Lambda expressions and expression trees
Type inference and overloading changes
Query expressions (from x in y where z select foo etc)

<shameless plug>
Find out more details in my book, C# in Depth:
http://manning.com/skeet
</shameless plug>

(Seriously though, if you want more information on any of those, just
let me know.)
 
S

Scott Roberts

Jon Skeet said:
Here we go:

Partial methods
Automatically implemented properties
Implicitly typed local variables
Object initializers
Collection initializers
Implicitly typed arrays
Anonymous types
Extension methods
Lambda expressions and expression trees
Type inference and overloading changes
Query expressions (from x in y where z select foo etc)

<shameless plug>
Find out more details in my book, C# in Depth:
http://manning.com/skeet
</shameless plug>

(Seriously though, if you want more information on any of those, just
let me know.)

I'd like to know your opinions on those features as they relate to
code-readability and maintainability (i.e. do they help, hurt, or have no
effect).
 
C

clintonG

Actually, if you learned how to learn you would be much better off than you
sound to be at the moment. The search engines support command lines and
Microsoft does use certain keywords and terms such as "What's New" and
another of my favorites "overview" to name their documents so try this

//search.live.com
what's new c# site:msdn2.microsoft.com

I thought I had a copy at one time but whay I would like to find --again--
is the document that lists the keywords and terms Microsoft uses for
documentation at MSDN, MSDN2 and let's not forget Technet while we're at it
eh?

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/
 
J

Jon Skeet [C# MVP]

I'd like to know your opinions on those features as they relate to
code-readability and maintainability (i.e. do they help, hurt,
or have no effect).

When used appropriately, they can help a lot - but only when you
actually *know* the new language features. For example, until you've
learned how query expressions are handled by the compiler, you can
easily make mistakes and the whole thing is somewhat magic. When you
*do* know what's going on behind the scenes, they're a huge boon to
readability, IMO.

I've been thinking a lot recently about two different aspects of
readability: what your code does, and how it does it. Often an increase
in the former aspect means a decrease in the latter. Extension methods
are a good example of that - they allow you to reduce the extra clutter
of specifying where a static method is coming from, but at the cost of
it not being obvious exactly what's going on.

That's okay so long as all the maintainers know how to find out the
"how" when they need to - but it also increases the risk of "cargo cult
programming":

http://blogs.msdn.com/ericlippert/archive/2004/03/01/syntax-semantics-
micronesian-cults-and-novice-programmers.aspx


A lot of the features of C# 3 could be overused and cause chaos - but
in the right hands they'll contribute a lot to readability, in my view.
I also suspect that a lot of the fear over some of the features
(implicitly typed local variables, for example) stems from a "but it's
never been like that before" viewpoint. I haven't been immune to this -
I initially hated the idea of implicitly typed local variables other
than with respect to anonymous types, but I now welcome them in many
other situations.

One thing I *would* say about C# 3 is that it's been very, very well
thought out. (That's not to say there aren't things I'd change - the
discovery of extension methods being the most obvious one.) The whole
thing hangs together as a set of changes and is very elegant. I've been
fortunate to spend rather a long time poring over specs etc (as part of
the writing process) which has made me think about the language changes
in a deeper way than I would probably have done otherwise.

In short, I really, really like C# 3 :)
 
S

Scott Roberts

I've been thinking a lot recently about two different aspects of
readability: what your code does, and how it does it. Often an increase
in the former aspect means a decrease in the latter. Extension methods
are a good example of that - they allow you to reduce the extra clutter
of specifying where a static method is coming from, but at the cost of
it not being obvious exactly what's going on.

That's okay so long as all the maintainers know how to find out the
"how" when they need to - but it also increases the risk of "cargo cult
programming":

http://blogs.msdn.com/ericlippert/archive/2004/03/01/syntax-semantics-
micronesian-cults-and-novice-programmers.aspx


A lot of the features of C# 3 could be overused and cause chaos - but
in the right hands they'll contribute a lot to readability, in my view.
I also suspect that a lot of the fear over some of the features
(implicitly typed local variables, for example) stems from a "but it's
never been like that before" viewpoint. I haven't been immune to this -
I initially hated the idea of implicitly typed local variables other
than with respect to anonymous types, but I now welcome them in many
other situations.

I suppose the onus of creating "readable" code always falls to the
individual developer. I just wonder to what extent some language features
invite or encourage "lazy" coding. I'm not an early adopter of new language
features, but I don't think I'm afraid of them either. I am, however, a
little scared when they come with names like "partial methods" (where is the
rest of it?), "Implicitly typed variables" (what's wrong with explicit
types?), and "anonymous types" (you don't want me to know what type it is?).

I'm sure that there is a time and a place for these features, just like
there is probably a time and a place for overloading the "+" operator to
actually perform subtraction in C++. But is it worth it? I suppose that's
not only a subjective and rhetorical question, but one's answer probably
changes over time. I read some C# code today where all of the local
variables were declared (but not initialized) at the top of the routine. I
chuckled, because I used to do that.
 
J

Jon Skeet [C# MVP]

I suppose the onus of creating "readable" code always falls to the
individual developer.
Agreed.

I just wonder to what extent some language features
invite or encourage "lazy" coding.

Some do - but some have the opposite effect. Consider automatically
implemented properties. A lazy programmer might normally decide to
expose a public field:

public class Person
{
public string Name;
}

They might think, "I know it *should* be a property, but it doesn't
hurt to be a field and the property would just be boilerplate code
anyway, adding to code bloat without doing anything."

With C# 3 they can write:

public class Person
{
public string Name { get; set; }
}

It's really, really easy to turn it into a property, which gives you
the correct encapsulation from the start, and extra code can be added
(e.g. for validation) without changing the actual API.
I'm not an early adopter of new language
features, but I don't think I'm afraid of them either. I am, however, a
little scared when they come with names like "partial methods" (where is the
rest of it?)

Partial methods are to do with partial types - hard to explain very
briefly, I'm afraid.
"Implicitly typed variables" (what's wrong with explicit
types?)

Well:

Dictionary<string,IList<string>> phoneNumberMap =
new Dictionary<string,IList<string>>();

isn't as nice in my view as:

var phoneNumberMap = new Dictionary<string,IList<string>>();

The latter has less redundancy.

It's also necessary for anonymous typing...
and "anonymous types" (you don't want me to know what type it is?).

Often in LINQ, you end up wanting to encapsulate things just for the
purposes of one method. The compiler can build a type for you in order
to do that simply, and you can always change it into a "real" type
later if you really want.

To be honest, I'm unlikely to convince you in the time I've got for
this post - it's worth seeing genuine code to understand the benefits.
I'm sure that there is a time and a place for these features, just like
there is probably a time and a place for overloading the "+" operator to
actually perform subtraction in C++. But is it worth it? I suppose that's
not only a subjective and rhetorical question, but one's answer probably
changes over time.

The interesting thing to me is that C# 3 encourages a whole shift of
mindset, to a more functional viewpoint - where it's useful, of course.
I think it will be a while before most developers (myself included) get
the most out of the new features. It can be really lovely though.
I read some C# code today where all of the local
variables were declared (but not initialized) at the top of the routine. I
chuckled, because I used to do that.

Yes, it's kinda sad how easy it is to move idioms over from one
language to another for no reason. Likewise I sometimes see C# code
such as:

if (5==i)

or

if (null==foo)

I know why people do that in C++, but the reason is gone for almost
every situation in C#. It's less readable than putting the constant
second, so why propagate the habit?
 
C

Chris Mullins [MVP - C#]

Scott Roberts said:
I'd like to know your opinions on those features as they relate to
code-readability and maintainability (i.e. do they help, hurt, or have no
effect).

I've been very happy with the overall readability improvements in my code
due to C# 3.

- The "intent" of my code has become much more clear. It's very easy to look
at a block of code and tell what that code should be doing. This is a very
positive thing, and is a direct result of many of the new language
features.

- The line-by-line understanding of my code has decreased, I believe. I
blame Lambda functions, and (most recently) custom iterators mixed with LINQ
and LINQ to SQL.

By this I mean, you may look at someone's code that implemented, say, a
QuickSort. You may unsterstand every single line of code, with excellent
clarity. The overall intent of the code though may remain elusive.

With the somewhat abusive way I've been using C# 3 these days, my intent is
typically very, very clear. However, some of the individual lines of code
are very difficult to understand. This is especially true of the reader
doesn't know C# 3.

Now I just need to start adding in some addition complexity:
yield return myFile.BeginWrite( Parallel.For(myBuffer,
d=>SomeOperation(d)), myBuffer.Count(d=>d.Value % 2 == 0), this, null));

(I'm kidding I'm kidding. I would never write code like that. Except on a
Friday afternoon.)
 
S

Scott Roberts

Now I just need to start adding in some addition complexity:
yield return myFile.BeginWrite( Parallel.For(myBuffer,
d=>SomeOperation(d)), myBuffer.Count(d=>d.Value % 2 == 0), this, null));

(I'm kidding I'm kidding. I would never write code like that. Except on a
Friday afternoon.)

I think that's why I'm gun-shy. I read macro-compiled Clipper code on a
somewhat frequent basis and it looks just like that!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top