There are many features like Auto implemented property. Anonymous types, Partial methods, object initializes, implicitly typed local variables.
Auto Implemented Property
Auto Implemented Property
The property plays a significant role to set and get the values. In the previous versions we were doing like need to set the values to the temporarily local variable. Now that work exactly reduced in the .NET 3.0 version. Because it will be implicitly handled by the .Net compiler
Syntax :
[Access modifier] data type [Name of property]
{
get { };
set { };
}
For example we are going to create one property for send email
public string FromID {get; set ;}
public string ToID {get; set ;}
public string Subject {get; set ;}
public string Message {get; set ;}
In previous version we have done like this.
private string fname;
public string FirstName
{
get
{
return fname
}
set
{
fname = value;
}
}
Here in .NET 3.0, it became auto implemented property, that means in the runtime it will put one temp variable for assigning the values by the compiler.
Implicit Typed local variable
The new data type introduced in the .NET 3.0. Normally when you store the elements in the string with the integer value we need to do the type casting. The variable that will be declared with the var keyword and it will be inferred by the compiler at the time of execution.
var Total = 10 + 10;
var iTotal = 10 + 15.5;
var Name = "deepak" + "dwij";
var Name = "deepak" + 10;
It can be used to create an array in the similar way.
There are some restrictions while using this feature
[Access modifier] data type [Name of property]
{
get { };
set { };
}
For example we are going to create one property for send email
public string FromID {get; set ;}
public string ToID {get; set ;}
public string Subject {get; set ;}
public string Message {get; set ;}
In previous version we have done like this.
private string fname;
public string FirstName
{
get
{
return fname
}
set
{
fname = value;
}
}
Here in .NET 3.0, it became auto implemented property, that means in the runtime it will put one temp variable for assigning the values by the compiler.
Implicit Typed local variable
The new data type introduced in the .NET 3.0. Normally when you store the elements in the string with the integer value we need to do the type casting. The variable that will be declared with the var keyword and it will be inferred by the compiler at the time of execution.
var Total = 10 + 10;
var iTotal = 10 + 15.5;
var Name = "deepak" + "dwij";
var Name = "deepak" + 10;
It can be used to create an array in the similar way.
There are some restrictions while using this feature
- We cannot do the increment and decrement operation like i++ or ++i .
- We cannot declare the NULL value to the var variable.
- It use be declare and initialize on the same statement in the local scope.
- We cannot initialize the multiple var variables like others. Int I, j, k;.
- We can't use the static keyword.
Implicitly Typed Arrays
var numbers = new[] { 1, 2, 3, 4, 5};
var names = new[] { "Deep", Doug, "Jim" };
Anonymous Types
An anonymous type is one of the new features introduced in the .NET 3.0 version. It is a read only property and can be used to assign the set of names constants with the values. These values cannot be changed the outside Anonymous type.
var Names = new
{
FirstName = "deepak",
LastName = "dwij",
Age = "25"
};
It can be accessed with the following:
string fname = Names.FirstName;
Normally when we define in the enum, there we cannot assign any values.
It will take the order from 0 and will be incremented by one for the every constants in the enumlist.
Here the value can be assigned.
Extension Methods(3.5 new feature)
Declaring Extension Methods:- We can create the extension of any existing method.Extension method behavior is similar to that of static methods.
You can declare them only in static classes. To declare an extension method, you specify the keyword this as the first parameter of the method, for example:
// Program.cs
public static class EMClass
{
public static int ToInt32Ext(this string s)
{
return Int32.Parse(s);
}
public static int ToInt32Static(string s)
{
return Int32.Parse(s);
}
}
class Program
{
static void Main(string[] args)
{
string s = "9";
int i = s.ToInt32Ext(); // LINE A
Console.WriteLine(i);
int j = EMClass.ToInt32Static(s); // LINE B
Console.WriteLine(j);
Console.ReadLine();
}
}
As you can see from the above snippet, the differences between the extension method (ToInt32Ext) and the regular static method (ToInt32Static) are the following:
var numbers = new[] { 1, 2, 3, 4, 5};
var names = new[] { "Deep", Doug, "Jim" };
Anonymous Types
An anonymous type is one of the new features introduced in the .NET 3.0 version. It is a read only property and can be used to assign the set of names constants with the values. These values cannot be changed the outside Anonymous type.
var Names = new
{
FirstName = "deepak",
LastName = "dwij",
Age = "25"
};
It can be accessed with the following:
string fname = Names.FirstName;
Normally when we define in the enum, there we cannot assign any values.
It will take the order from 0 and will be incremented by one for the every constants in the enumlist.
Here the value can be assigned.
Extension Methods(3.5 new feature)
Declaring Extension Methods:- We can create the extension of any existing method.Extension method behavior is similar to that of static methods.
You can declare them only in static classes. To declare an extension method, you specify the keyword this as the first parameter of the method, for example:
// Program.cs
public static class EMClass
{
public static int ToInt32Ext(this string s)
{
return Int32.Parse(s);
}
public static int ToInt32Static(string s)
{
return Int32.Parse(s);
}
}
class Program
{
static void Main(string[] args)
{
string s = "9";
int i = s.ToInt32Ext(); // LINE A
Console.WriteLine(i);
int j = EMClass.ToInt32Static(s); // LINE B
Console.WriteLine(j);
Console.ReadLine();
}
}
As you can see from the above snippet, the differences between the extension method (ToInt32Ext) and the regular static method (ToInt32Static) are the following:
- Extension methods have the keyword this before the first argument. Static methods do not have the this keyword in its argument declaration.
- When extension methods are consumed, the argument that was declared with the keyword this is not passed. In the above code, Line A is an example of consuming the extension method ToInt32Ext. No argument is passed to it. When static methods are consumed, no arguments are skipped. All expected arguments must be entered. Line B is an example of this.
- Extension methods can be defined only in a static class. For static methods, this is not a requirement. Static methods can exist in a regular class as well as in a static class.
- Extension methods can be called only on instances values.
Object and Collection Initializers
.NET 3.0 is expected to allow you to include an initializer that specifies the initial values of the members of a newly created object or collection. This enables you to combine declaration and initialization in one step.
For instance, if you defined a Co-Ordinate class as follows:
public class CoOrdinate
{
public int x ;
public int y;
}
You then could declare and initialize a CoOrdinate object using an object initializer, like this:
var myCoOrd = new CoOrdinate{ x = 0, y= 0} ;
List<string> animals = new List<string>();
animals.Add("monkey");
animals.Add("donkey");
animals.Add("cow");
animals.Add("dog");
animals.Add("cat");
Now can be shortened to simply:
List<string> animals = new List<string> {
"monkey", "donkey", "cow", "dog", "cat" } ;
Lambda Expressions: The Espresso of Anonymous Methods
A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types. Lambda Expressions provide a more concise, functional syntax for writing anonymous methods.
All lambda expressions use the lambda operator =>, which is read as "goes to".
Earlier:
class Program
{
delegate void DemoDelegate();
static void Main(string[] args)
{
DemoDelegate myDelegate = new DemoDelegate(SayHi);
myDelegate();
}
void SayHi()
{
Console.Writeline("Hi Deepak!!") ;
}
}
In 2.0(Annonymous method)
class Program
{
delegate void DemoDelegate();
static void Main(string[] args)
{
DemoDelegate myDelegate = delegate()
{
Console.Writeline("Hi Deepak!!");
};
myDelegate();
}
}
In .NET 3.0
You can write a lambda expression as a parameter list, followed by the => token, followed by an expression or statement block. The above code can now be replaced with the following code:
class Program
{
delegate void DemoDelegate();
static void Main(string[] args)
{
DemoDelegate myDelegate = () => Console.WriteLine("Hi Deepak!!") ;
myDelegate();
}
}
No comments:
Post a Comment