Here's list of new features of C# language in Framework 3.0.
1. Implicitly Typed Local Variables
You can use implicitly-typed local variables to store anonymous types. You can also use them in any other situation in which you want the compiler to determine the type of a local variable (in other words a variable declared at method scope). The following examples show how to use implicitly typed variables in both scenarios.
var developerNames =
from developer in developers
where developer.Language.Equals("C#")
select developer.FirstName + " " + developer.LastName;
foreach (string str in developerNames)
{
Console.WriteLine(str);
}
2. Implicitly Typed Arrays
You can also create an implicitly-typed array in which the type of the array instance is inferred from the elements specified in the array initializer. The rules for any implicitly-typed variable also apply to implicitly-typed arrays.
var ourSite = new[] { "asp", "net", "source", ".", "com" };
3. Auto-Implemented Properties
Auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.
public class Developer
{
public string Language { get; set; }
public string Name { get; set; }
}
4. Additional Object Initializers Way
You can initialize new objects in easiest way:
...
Developer dev = new Developer { Language = "C#", Name = "Ivanov" };
5. Additional Arrays and Collections Initializers Way
Developer[] devs = new Developer[] {
new Developer { Language = "C#", Name = "Tihomir" },
new Developer { Language = "VB.NET", Name = "Ivanov" }
};
6. Extension Methods
Now you can "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.
public static bool IsDotNetDeveloper(this Developer dev)
{
return dev.Language.Equals("C#") || dev.Language.Equals("VB.NET");
}
...
Developer dev = new Developer { Language = "C#", Name = "Ivanov" };
bool isDotNet = dev.IsDotNetDeveloper();
7. Anonymous types
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type. The type name is generated by the compiler and is not available at the source code level.
var v = new { Language1 = "C#", Language2 = "VB.NET" };
8. Lambda Expressions
When '=>' is used that's 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.
string[] languages = { "C++", "C#", "VB.NET" };
int min = languages.Min(l => l.Length);
9. Query Keywords
- from - Specifies a data source and a range variable (similar to an iteration variable).
- where - Filters source elements based on one or more Boolean expressions separated by logical AND and OR operators ( && or || ).
select - Specifies the type and shape that the elements in the returned sequence will have when the query is executed.
group - Groups query results according to a specified key value.
into - Provides an identifier that can serve as a reference to the results of a join, group or select clause.
orderby - Sorts query results in ascending or descending order based on the default comparer for the element type.
join - Joins two data sources based on an equality comparison between two specified matching criteria.
let - Introduces a range variable to store sub-expression results in a query expression.
string[] languages = { "C++", "C#", "VB.NET" };
var dotnets = from lang in languages
where lang.Equals("C#") || lang.Equals("VB.NET")
select lang;
foreach (var dotnet in dotnets)
{
Console.WriteLine(dotnets);
}
10. Partial Method
A partial method has its signature defined in one part of a partial type, and its implementation defined in another part of the type. Partial methods enable class designers to provide method hooks, similar to event handlers, that developers may decide to implement or not. If the developer does not supply an implementation, the compiler removes the signature at compile time.
partial class YourClass
{
partial void SomeMethod(int i);
}
// This part can be in a separate file.
partial class YourClass
{
// Comment out this method and the program
// will still compile.
partial void SomeMethod(int i)
{
Console.WriteLine("AspNetSource.com visit #{0}", i);
}
}
That's All. Happy Coding :)