Geeks With Blogs
Sudheer Kumar ASP.Net, C#, BizTalk, MSBuild, WPF, WCF, WF....
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.... Posted on Thursday, March 27, 2008 6:21 PM .Net 3.0/3.5 | Back to top


Comments on this post: C# 3.5 Cool Features:

# re: C# 3.5 Cool Features:
Requesting Gravatar...
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.
Left by Matt on Jun 12, 2008 2:51 PM

# re: C# 3.5 Cool Features:
Requesting Gravatar...
Thanks for highlighting the new features, interesting article.

-Joe
Left by Joe Marinaccio on Nov 20, 2008 1:52 PM

# re: C# 3.5 Cool Features:
Requesting Gravatar...
You can make properties to be read-only to external classes:

public string GetName { get; private set; }
Left by Andy on Nov 27, 2008 9:34 AM

# re: C# 3.5 Cool Features:
Requesting Gravatar...
nice tips ....
Left by senthil athi on Apr 26, 2009 3:44 PM

# re: C# 3.5 Cool Features:
Requesting Gravatar...
Nice article .
Some of the features are:
Object Intializers
Collection Initializers
Lambda Expression
Left by AK on May 05, 2009 12:51 AM

# re: C# 3.5 Cool Features:
Requesting Gravatar...
It is not what I expected
Left by Sunil on Sep 06, 2009 10:40 AM

# re: C# 3.5 Cool Features:
Requesting Gravatar...
u given the c#.net 3.0 features,

Left by upen on Oct 20, 2009 11:40 PM

# re: C# 3.5 Cool Features:
Requesting Gravatar...
continue please!
Left by itiwant on Jan 08, 2010 5:34 AM

# re: C# 3.5 Cool Features:
Requesting Gravatar...
More clarification is required
Left by ABCD on Apr 09, 2010 7:01 AM

# re: C# 3.5 Cool Features:
Requesting Gravatar...
.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.

Left by Chaitanya on Mar 08, 2011 12:22 AM

# re: C# 3.5 Cool Features:
Requesting Gravatar...
It is not what I expected
Left by ice maker on Apr 01, 2011 3:14 AM

Your comment:
 (will show your gravatar)


Copyright © Sudheer Kumar | Powered by: GeeksWithBlogs.net