2

I know very little about C# or .net but I am interested in learning about them.

One thing that intrigues me is that I keep hearing that "C# 3 is really great".
Why is that? What is the difference from C# 2. Are the differences just in C# or in .net also?

Thanks in advance.

nash
  • 3,020
  • 6
  • 38
  • 53
  • 1
    There are plenty of other posts dealing with this http://stackoverflow.com/questions/237908/differences-between-net-versions-predominantly-c http://stackoverflow.com/questions/170584/c-2-0-to-c-3-0-worth-it http://stackoverflow.com/questions/173080/c-net-3-0-3-5-features-in-2-0-using-visual-studio-2008/659460 – womp Jul 18 '09 at 02:49

9 Answers9

11

I have a little article about this: the Bluffer's Guide to C# 3. Obviously there are more details in my book but it should be enough to get you going. In short:

  • Automatically implemented properties:

    public int Value { get; set; }
    
  • Object and collection initializers:

    Form form = new Form { Size = new Size(100, 100),
                           Controls = { new Label { Text = "Hi" } }
                         };
    List<string> strings = new List<string> { "Hi", "There" };
    
  • Implicitly typed local variables:

    var x = new Dictionary<string, int>(); // x is still statically typed
    
  • Implicitly typed arrays:

    DoSomething(new[] { "hi", "there"}); // Creates a string array
    
  • Anonymous types:

    var jon = new { Name = "Jon", Age = 33 };
    
  • Lambda expressions (like anonymous methods but shorter):

    Func<string, int> lengthFunc = x => x.Length;
    
  • Expression trees:

    // Representation of logic as data
    Expression<Func<string, int>> lengthExpression = x => x.Length;
    
  • Extension methods: (static methods which act like instance methods on the type of their first parameter)

    public static string Reverse(this string text)
    {
        char[] chars = text.ToCharArray();
        Array.Reverse(chars);
        return new string(chars);
    }
    
    ...
    
    string hello = "olleh".Reverse();
    
  • Query expressions:

    var query = from person in people
                where person.Age > 18
                select person.Name;
    
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

You can read all about what was introduced in C# 3.0 in the Wikipedia article.

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
3

This is a too big question, the best source to answer your question is C# in depth by Jon Skeet

Community
  • 1
  • 1
J.W.
  • 17,991
  • 7
  • 43
  • 76
3

The most prominent theme of C# 3.0 was data, which is realized through Language Integrated Query (LINQ). Most of the other language features, such as implicitly typed local variables, anonymous types, lambda expressions, and extension methods were added to support LINQ. LINQ is a way to write SQL-like statements in your code to query multiple types of data sources. What is attractive about this is that you now have a common way to write code to access different data sources, meaning that you don't have to completely re-learn the wheel every time MS introduces a new data access technology or you need to use a 3rd party data source. Since most of the major database vendors are supporting LINQ in some way, the benefits of this common way to query data is being realized today.

Some people like LINQ and others don't. I'm in the camp that believes it is a great addition to the language, but you should look at your own requirements and situation and make an informed decision on whether adopting C# 3.0 is right for you.

2

Here's a list of some of the new stuff in 3.0. Lots of great stuff.

JP Alioto
  • 44,864
  • 6
  • 88
  • 112
2

Linq, lambda, var keywork, desition trees, anonymous objects, shorter property syntax, object initializers, collection initializers, Extension Methods.

But Lambda and Extension Methods are the big ones. C# 3.0 is amazingly good.

Chris Brandsma
  • 11,666
  • 5
  • 47
  • 58
2

The differences are really in C# and not (just) in .NET.

The fun thing is, with Visual Studio 2008 multi-targeting you can use most of the nifty stuff from C# 3.0 in a .NET 2.x project previously limited to C# 2.0.

You don't get

  • Expression tree conversions,
  • Linq Query Keywords, [Skeet says you can...]

but you do get

  • Lamda expressions [so says Skeet]
  • Collection and Object Initializers
  • Implicitly typed local variables and Anonymous Types i.e. var
  • Auto-Implemented Properties and
  • Partial Method Definitions

So, just start using the new syntactic sugar.

dlamblin
  • 43,965
  • 20
  • 101
  • 140
  • Actually lambda expressions work when targeting .NET 2, but not expression tree conversions. LINQ query expressions will work if you have the right supporting members, e.g. due to LINQBridge. – Jon Skeet Oct 09 '10 at 08:38
1

There are plenty of articles on this topic:

See: http://www.developer.com/net/csharp/article.php/3561756

Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
0

The evolution of C# looks like this:

  • 1.0: Managed code
  • 2.0: Generics
  • 3.0: LINQ
  • 4.0: Dynamic programming
  • 5.0: Asynchronous programming

See slide 5 in the presentation The Future of C# and Visual Basic by Anders Hejlsberg from PDC 10.

sourcenouveau
  • 29,356
  • 35
  • 146
  • 243