Sandip's Programming Zen

An attempt to share tech/coding experiences

Posts Tagged ‘C# 3.0

C# 3.0 Language Enhancements

leave a comment »

I am trying to summarize some of the most visible new C# 3.0 new language features. Here they go…

Automatic properties

Create properties in a single line

public string FirstName { get; set; }

  • No internal field required
  • Similar to property interface definition
  • Easy to upgrade when logic is required

Object initialisers

Quickly initialize properties on new objects

User user = new User { FirstName=”JD” };

  • Provides short hand object creation

Collection Initialisers

Similar to object initialisers

List<User> users = new List<User>
{
new User(),
new User()

};

Extension methods

Add new methods to existing types

public static bool ReturnTrue(this string s)
{
return true;
}

  • Specific to a given namespace
  • Potential upgrade path issues

Implicit types

Infer a type

var number = 600;

int number = 600;

Type is defined at compile time (statically type)

Partial Methods

  • Methods in partial classes
    • One with implementation
    • One without implementation
  • Primary designed for code-gen

partial void MyMethod();

partial void MyMethod()

{

//method body

}

Lambda Expressions

  • Powerful addition for querying
  • Lambda calculus
  • Introduces the => operator

U => U.FirstName.Length > 5

  • Requires value on variable creation
  • Required for anonymous types

LINQ

  • Language INtegrated Query
  • Provides new syntax for querying using Lambda Expressions

    If you know any more interesting features then put them in your comments here.

    Advertisements

    Written by Sandip

    February 2, 2008 at 7:08 pm

    Posted in .Net

    Tagged with , , ,