Implicitly typed local variables Implicitly typed arrays Auto Implemented Properties Extension methods Lambda expressions Object initializers Anonymous types Query expressions Expression trees
C# 3.0 introduces a new keyword called "var".
This new keyword enables you to declare a variable whose type is implicitly inferred from the expression used to initialize the variable.
For example consider the following line of code:
var age = 30;
The preceding line initializes the variable age to
value 30 and automatically gives it the type of integer. Note that the variable age is a strongly typed variable (integer in this case) and doesnt carry the overhead of a generic object type such as System.Object class.
The declarator must include an initializer:
ERROR: var x;
The initializer must be an expression.
The initializer cannot be an object or collection initializer by itself but it can be a new expression that includes an object or collection initializer. ERROR: var y = {1 2 3}; cannot be the null type. ERROR: var z = null;
The compile-time type of the initializer expression
If the local variable declaration includes multiple
declarators the initializers must all have the same compile-time type. ERROR: var t = Hello; t = 3;
In addition to implicitly declaring
variables you can also use the var keyword for declaring arrays as well
var numbers = new[] { 1 2 3 4 5}; // int[]
array elements at compile-time by examining the values from the initialization expression.
Whenever you declare a class most of the times the
class is used only as a placeholder with getters and setters for holding property values without any additional logic. And now: public class Person { public int ID { get; set; } public string FirstName { get; set; } public string LastName { get; private set; } public string FullName { get { return FirstName + " " + LastName; } } }
Extension methods are static methods that
can be invoked using instance method syntax. In effect extension methods make it possible to extend existing types and constructed types with additional methods. public static class ExtermalObjectMethods { public static bool IsNull(this object obj) { return obj == null; } } Import ExtermalObjectMethods ; object o = null; if (o.IsNull() ) { Console.WriteLine("Object is null"); }
Like anonymous methods (delegate)
only with an even easier syntax. A lambda expression is written as parameters => expression Example: var i = 1; Func f = j => j + 1; Console.Out.WriteLine(f(i));
namespace System.Query
public static class Sequence { public static IEnumerable Select( this IEnumerable source Func selector) { foreach (T element in source) yield return selector(element); } } } List customers = GetCustomerList(); IEnumerable names =
{
Func fib = null;
fib = n => n > 1 fib(n - 1) + fib(n 2) : n; Console.Out.WriteLine(fib(90)); //10946
Not good example though because
recursion is VERY memory consuming in compare to iteration. Should use memoizing if you still
An object initializer specifies values for one or
more fields or properties of an object. Example: public class Point { int x y; public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } } An instance of Point can be created an initialized as follows: var a = new Point { X = 0 Y = 1 }; which has the same effect as var a = new Point(); a.X = 0;
A collection initializer specifies the elements of a
collection. Example: public class Contact { string name; List phoneNumbers = new List(); public string Name { get { return name; } set { name = value; } } public List PhoneNumbers { get { return phoneNumbers; } } }
A List can be created and initialized as follows: var contacts = new List {
new Contact {
882-8080" }} new Contact {Name = "Bob Harris" PhoneNumbers = { "650-555-0199" } } }; which has the same effect as var contacts = new List(); var __c1 = new Contact(); __c1.Name = "Chris Smith"; __c1.PhoneNumbers.Add("206-555-0101"); __c1.PhoneNumbers.Add("425-882-8080"); contacts.Add(__c1); var __c2 = new Contact(); __c2.Name = "Bob Harris"; __c2.PhoneNumbers.Add("650-555-0199"); contacts.Add(__c2); where __c1 and __c2 are temporary variables that are otherwise invisible and inaccessible
Name = "Chris Smith" PhoneNumbers = { "206-555-0101" "425-
anonymous types allow you to create
a type on-the-fly at compile time. The newly created type has public properties and backing fields defined for the members you initialize during construction: var obj= new{ID=1FirstName=Oz"LastNam e=Bar"}; Passing of an Anonymous Type is by first cast the type to object
Query expressions provide a
language integrated syntax for queries that is similar to relational and hierarchical query languages such as SQL. The tr