around the C# language enhancements in .NET 3.5.
- Local Variable Type Inference:
Type inference allowing you to Declaring a variable without specifying it's type.
However, these variables are also strong typed, because the compiler infers the
variable type by the
value you initialize it with.
Example:
var IntVar=1; //int type
var StringVar="Hello, World"; //string type
var DitionaryOfLists=new Dictionary< string,List< int>>();
the C# compiler is smart enough to recognize the type of each variable of this depending on the initialization value you specified for this variable.
Note:this method working only for local variables,
also this variables must have initialization value to specify it's type.
- Automatically Implemented Properties
Oftentimes property accessors (get and set) have trivial implementations and follow the pattern that
simply get (return) a private field and set the private field to the value passed.
Automatically Implemented Properties provide a more concise syntax for implementing this pattern,
where the C# compiler automatically generates the backing fields.
Example:
// that's the old style of the properties implementation
public class Point
{
private int x;
private int y;
public int X { get { return x; } set { x = value; } }
public int Y { get { return y; } set { y = value; } }
}
// that's the new style that will automatically generates your private class
// member and your public property
public class Point
{
public int X { get; set; }
public int Y { get; set; }
}
we can also use Accessibility Modifiers with Properties.
suppose that in the last example we want the X variable to be read only we can simply let it's set accessor to be private so that it will not be available to specify a value for it outside the class.
Example:
public class Point
{
public int X { get;Private set; }
public int Y { get; set; }
}
- Easy Initialization with Object and Collection Initializers
In C# 3.0, when declaring an object or collection, you may include an initializer that specifies the
initial values of the members of the newly created object or collection. This new syntax combines
object creation and initialization into a single step.
Example:
// suppose again the Point class in the previous example
// if we need to Declare an object of it with initialization values
// we can do that
Point p = new Point { X = 3, Y = 99 };
that's equivalent to defining a constructor for the class that have properties members of the class as it's parameters and invoking it in the object declaration and assign a value to it.
also With C# 3.0, any object that implements
System.Collections.Generic.IEnumerable
and has a public Add method can have its values initialized with a collection
initializer.
Example:
//Using the Point class, let’s create a shape
// that is made up of a collection of points.
List
{
new Point { X=0, Y=5 },
new Point { X=5, Y=5 },
new Point { X=5, Y=0 },
new Point { X=0, Y=0 }
};
i think that's enough for now we will complete in the next article.
have a nice day and enjoy.
No comments:
Post a Comment