1. Use the VaR keyword to implement flexible type declaration:
Class implicitlytypedlocals2
{
Static void main ()
{
String [] words = {"apple", "blueberry", "Cherry "};
// If a query produces a sequence of anonymous types,
// Then you must also use VaR in the foreach statement.
VaR upperlowerwords =
From W in words
Select New {upper = W. toupper (), lower = W. tolower ()};
// Execute the query
Foreach (VAR ul in upperlowerwords)
{
Console. writeline ("uppercase: {0}, lowercase: {1}", ul. Upper, ul. Lower );
}
}
}
2. More flexible object construction methods:
Private class cat
{
// Auto-implemented Properties
Public int age {Get; set ;}
Public string name {Get; set ;}
}
Static void methoda ()
{
// Object initializer
Cat cat = new cat {age = 10, name = "Sylvester "};
}
3. Special Methods for assembly:
Ienumerable <int> highscoresquery =
From score in scores
Where score> 80
Orderby score descending
Select score;
4. Extend methods for existing type definitions:
Namespace extensionmethods
{
Public static class myextensions
{
Public static int wordcount (this string Str)
{
Return Str. Split (New char [] {'', '.', '? '}, Stringsplitoptions. removeemptyentries). length;
}
}
}
Using extensionmethods;
String S = "Hello extension methods ";
Int I = S. wordcount ();
5. implicit set type definition:
VaR v = new {amount = 108, message = "hello "};
VaR productquery =
From prod in products
Select New {prod. Color, prod. Price };
Foreach (var v in productquery)
{
Console. writeline ("color = {0}, price = {1}", V. Color, V. Price );
}
6. Lambda Syntax:
Delegate int del (int I );
Del mydelegate = x => X * X;
Int J = mydelegate (5); // J = 25
7. query syntax:
Class lownums
{
Static void main ()
{
// A simple data source.
Int [] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
// Create the query.
// Lownums is an ienumerable <int>
VaR lownums = from num in numbers
Where num <5
Select num;
// Execute the query.
Foreach (int I in lownums)
{
Console. Write (I + "");
}
}
}
// Output: 4 1 3 2 0
8. Smart and simple accessors:
Class lightweightcustomer
{
Public double totalpurchases {Get; set ;}
Public string name {Get; private set;} // read-only
Public int customerid {Get; private set;} // read-only
}
Lightweightcustomer OBJ = new lightweightcustomer ();
OBJ. totalpurchases = 1;
Console. writeline (obj. totalpurchases. tostring ());
9. Support partial declarations for excuses, classes, structures, methods, and features:
Partial class Earth: Planet, irotate {}
Partial class Earth: irevolve {}
[System. serializableattribute]
Partial Class moon {}
[System. obsoleteattribute]
Partial Class moon {}