……………………………………………….Expertise in .NET Technologies

What’s New in C# 3.0

Posted by Ravi Varma Thumati on May 20, 2009


C# is a general-purpose, type-safe, object-oriented programming language whose goal is programmer productivity. To this end, the language balances simplicity, expressiveness, and performance. The C# language is platform-neutral, but it was written to work well with the Microsoft .NET Framework. C# 3.0 targets .NET Framework 3.5.

What’s New in C# 3.0

C# 3.0 features are centered on Language Integrated Query capabilities or LINQ for short. LINQ enables SQL-like queries to be written directly within a C# program, and checked statically for correctness. Queries can execute either locally or remotely; the .NET Framework provides LINQ-enabled APIs across local collections, remote databases, and XML.

C# 3.0 features include:

  • Lambda expressions
  • Extension methods
  • Implicitly typed local variables
  • Query comprehensions
  • Anonymous types
  • Implicitly typed arrays
  • Object initializers
  • Automatic properties
  • Partial methods
  • Expression trees

Lambda expressions are like miniature functions created on the fly. They are a natural evolution of anonymous methods introduced in C# 2.0, and in fact, completely subsume the functionality of anonymous methods. For example:

               Func<int,int> sqr = x => x * x;
               Console.WriteLine (sqr(3));           // 9

The primary use case in C# is with LINQ queries, such as the following:

               string[] names = { "Tom", "Dick", "Harry" };
               // Include only names of >= 4 characters:
               IEnumerable<string> filteredNames =
                 Enumerable.Where (names, n => n.Length >= 4);

Extension methods extend an existing type with new methods, without altering the type’s definition. They act as syntactic sugar, making static methods feel like instance methods. Because LINQ’s query operators are implemented as extension methods, we can simplify our preceding query as follows:

               IEnumerable<string> filteredNames =
                 names.Where (n => n.Length >= 4);

Implicitly typed local variables let you omit the variable type in a declaration statement, allowing the compiler to infer it. Because the compiler can determine the type of filteredNames, we can further simplify our query:

               var filteredNames = names.Where (n => n.Length == 4);

Query comprehension syntax provides SQL-style syntax for writing queries. Comprehension syntax can simplify certain kinds of queries substantially, as well as serving as syntactic sugar for lambda-style queries. Here’s the previous example in comprehension syntax:

               var filteredNames = from n in names
                                   where n.Length >= 4
                                   select n;

Anonymous types are simple classes created on the fly, and are commonly used in the final output of queries:

               var query = from n in names where n.Length >= 4
                           select new {
                                        Name = n,
                                        Length = n.Length

Here’s a simpler example:

               var dude = new { Name = "Bob", Age = 20 };

Implicitly typed arrays eliminate the need to state the array type, when constructing and initializing an array in one step:

               var dudes = new[]
                  new { Name = "Bob", Age = 20 },
                  new { Name = "Rob", Age = 30 }

Object initializers simplify object construction by allowing properties to be set inline after the constructor call. Object initializers work with both anonymous and named types. For example:

               Bunny b1 = new Bunny {
                                       Name = "Bo",
                                       LikesCarrots = true,

The equivalent in C# 2.0 is:

               Bunny b2 = new Bunny( );
               b2.Name = "Bo";
               b2.LikesCarrots = false;

Automatic properties cut the work in writing properties that simply get/set a private backing field. In the following example, the compiler automatically generates a private backing field for X:

               public class Stock
                 public decimal X { get; set; }

Partial methods let an auto-generated partial class provide customizable hooks for manual authoring. LINQ to SQL makes use of partial methods for generated classes that map SQL tables.

Expression trees are miniature code DOMs that describe lambda expressions. The C# 3.0 compiler generates expression trees when a lambda expression is assigned to the special type Expression<TDelegate>:

               Expression<Func<string,bool>> predicate =
                 s => s.Length > 10;

Expression trees make it possible for LINQ queries to execute remotely (e.g., on a database server) because they can be introspected and translated at runtime (e.g., into an SQL statement).



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: