Showing posts with label .NET. Show all posts
Showing posts with label .NET. Show all posts

Wednesday, December 05, 2007

What's New in .Net 3.5

I have been checking about recently released .net 3.5 in these few days and there are many really pretty exciting features out there and also practically useful, applicable, lesser lines of code required and more productive in day to day works. I would say that Microsoft really made another major mile stone with .net framework achievement.

here are a few features I learned from the blogs and communities.

  1. Implicitly Type Local variables
  2. Anonymous Types
  3. Automatic Properties, Object Initializers, and Collection Initializers
  4. Extension Methods
  5. Lambda Expressions ( => )
  6. Language Integrated Query (LINQ)
  7. LINQ to SQL

1.Implicitly Type Local variables

Local variables can be given an inferred "type" of var instead of an explicit type. The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement.

Compiler works for you to convert to the right type during compilation and so, from IL perspective, it sees as the type converted by compiler.

// i is compiled as an int

var i = 5;

// s is compiled as a string

var s = "Hello";

// a is compiled as int[]

var a = new[] { 0, 1, 2 };

// expr is compiled as IEnumerable<Customer>

var expr =

from c in customers

where c.City == "London"

select c;

// anon is compiled as an anonymous type

var anon = new { Name = "Terry", Age = 34 };

// list is compiled as List<int>

var list = new List<int>();

2.Anonymous Types

Anonymous types are a convenient language feature of C# and VB that enable developers to concisely define inline CLR types within code, without having to explicitly define a formal class declaration of the type.

Anonymous types are particularly useful when querying and transforming/projecting/shaping data with LINQ.

var person = new { FirstName = "Kyaw", LastName="Oo", BirthPlace="Yangon" };


For more detail, read here.

3.Automatic Properties, Object Initializers, and Collection Initializers

Isn’t it cool if you can write the class like below without requiring the private member and the compiler will automatically create the private members and map these to properties?

No need to declare private member and compiler will do it for you. But later if you want to modify the property for adding extra validation codes, you still can do without breaking to the client codes.

Automatic Properties

public class Person {

// Auto-implemented properties

public string FirstName { get; set; }
public string LastName { get; private set; } //Read Only
public int Age { get; set; }

Object Initializers

New alternative way of initializing the object. Cool, huh?

Person person = new Person { FirstName="Scott", LastName="Guthrie", Age=32 };

Collection Initializers

For more detail, read here.

4.Extension Methods

In old days, you will write your validation codes like below.

string email = Request.QueryString["email"];
( EmailValidator.IsValid(email) ) {

Have you ever thought about that you could write something like in C#, VB.NET? Now, it’s possible in .Net 3.5 .

string email = Request.QueryString["email"];
if(email.IsValidEmailAddress()) {


For more detail, read here.

5.Lambda Expression ( “=>” )

The => token is called the lambda operator. It is used in lambda expressions to separate the input variables on the left side from the lambda body on the right side. Lambda expressions are inline expressions similar to anonymous methods but more flexible; they are used extensively in LINQ queries that are expressed in method syntax.

string[] words = { "cherry", "apple", "blueberry" };

int shortestWord = words.Min(w => w.Length);

The => operator is read as "goes to." In the previous example, the expression is read as “Min w goes to w dot Length”.

The => operator has the same precedence as the assignment operator (=) and is right-associative.

public class Person


public string FirstName { get; set; }

public string LastName { get; set; }

public int Age { get; set; }


6.Language Integrated Query (LINQ)

This is the most productive and very useful feature in .Net 3.5 and it gives the developer to save the tremendous development time on data access layer codes.

Language Integrated Query (LINQ, pronounced "link") is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages using a syntax reminiscent of SQL.


This is how LINQ supported for SQL database. The following link will give you the comprehensive detail of LINQ – SQL insight.

For further reading:

Have a nice day!

kick it on