Sudheer Kumar

ASP.Net, C#, BizTalk, MSBuild, WPF, WCF, WF....
posts - 28, comments - 111, trackbacks - 16

My Links

News



Archives

Post Categories

C# 3.5 Cool Features:

C# 3.5 provides a lot of good features and here are a few of them.

Implicitly Typed Local Variables:

The compiler derives the type from the initialized value.

// Implicitly typed local variables.

var myInt = 0;

var myBool = true;

var myString = "Time, marches on...";

 

These are greatly useful while using with LINQ.

Automatic properties:

No need to write the entire property syntax.

class Car

{

// Automatic property syntax.

public string PetName { get; set; }

}

 

·        But they cannot be used to define read-only or write only properties

// Read-only property? Error!

public int MyReadOnlyProp { get; }

 

·        Abstract Automatic Property can be defines as :

abstract class Car

{

// Abstract property in an abstract base class.

public abstract string PetName { get; set; }

}

 

·        Restricted Access to Automatic Properties:

Get can be public and set can be protected.

public string PetName { get; protected set; }

 

·        Default values are assigned automatically:

In the above case, PetName is assigned null when declared.
 

Extension Methods:

                                                               i.      This technique can be quite helpful when you need to inject new functionality into types for which you do not have an existing code base.

                                                             ii.      Extension methods are static methods and can be defined on any class for any other type.

The following code extends System.Object to have a new method.

static class MyExtensions

{

// This method allows any object to display the //assembly it is defined in.

public static void DisplayDefiningAssembly(this object obj)

{

Console.WriteLine("{0} lives here:\n\t->{1}\n", obj.GetType().Name,

Assembly.GetAssembly(obj.GetType()));

}

}

 

o   this on the first parameter mentions the extended method.

o   this can only be applied to first argument

o   Extension methods can take multiple arguments

 

                                                            iii.      Calling Extension Methods:

1.      On instance

int i = 0;

i.GetAssembly();

 

2.      Using static way:

MyExtensions.DisplayDefiningAssembly(myInt);

 

More on this later....

Print | posted on Thursday, March 27, 2008 6:21 PM | Filed Under [ .Net 3.0/3.5 ]

Feedback

Gravatar

# re: C# 3.5 Cool Features:

Just FYI, it's C# 3.0. Lots of people are confused though, since C# 3.0 came out the same time as .NET 3.5.
6/12/2008 2:51 PM | Matt
Gravatar

# re: C# 3.5 Cool Features:

Thanks for highlighting the new features, interesting article.

-Joe
11/20/2008 1:52 PM | Joe Marinaccio
Gravatar

# re: C# 3.5 Cool Features:

You can make properties to be read-only to external classes:

public string GetName { get; private set; }
11/27/2008 9:34 AM | Andy
Gravatar

# re: C# 3.5 Cool Features:

nice tips ....
4/26/2009 3:44 PM | senthil athi
Gravatar

# re: C# 3.5 Cool Features:

Nice article .
Some of the features are:
Object Intializers
Collection Initializers
Lambda Expression
5/5/2009 12:51 AM | AK
Gravatar

# re: C# 3.5 Cool Features:

It is not what I expected
9/6/2009 10:40 AM | Sunil
Gravatar

# re: C# 3.5 Cool Features:

u given the c#.net 3.0 features,

10/20/2009 11:40 PM | upen
Gravatar

# re: C# 3.5 Cool Features:

continue please!
1/8/2010 5:34 AM | itiwant
Gravatar

# re: C# 3.5 Cool Features:

More clarification is required
4/9/2010 7:01 AM | ABCD
Gravatar

# re: C# 3.5 Cool Features:

.Net is version 3.5, C# is version 3.0. The confusion came in when they introduced .Net 3.0


.Net 1.0 - C# 1.0

.Net 1.1 - C# 1.1

.Net 2.0 - C# 2.0

.Net 3.0 - C# 2.0

.Net 3.5 - C# 3.0

Please check which version the features came before posting on net.

3/8/2011 12:22 AM | Chaitanya
Gravatar

# re: C# 3.5 Cool Features:

It is not what I expected
4/1/2011 3:14 AM | ice maker
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: