New features and advantages of c#3.0 than c#2.0

Source: Internet
Author: User

C#3.0 has new features and advantages over c#2.0:

In c#3.0, some of the new features that Microsoft has brought to me may be features that all previous development languages did not have. This undoubtedly greatly embodies the strong advantage of c#3.0 in the development of language.

Lambda expression

A LAMBDA expression is an anonymous function that can contain expressions and statements and can be used to create delegates or expression tree types. All lambda expressions are => using the lambda operator. For a more detailed explanation of the lambda you can refer to MSDN. It's very clear in that.

Here's a simple example to illustrate the benefits of a lambda. Lambda provides a clearer way to implement anonymous delegation processing. For example, in 2.0. We can write code like this:

public class Example
{
public static void Demo (System.Windows.Controls.TextBlock outputblock)
{
Func<string, string> convert = Delegate (string s)
{return s.toupper ();};

String name = "Dakota";
Outputblock.text + = CONVERT (name) + "\ n";
}
}

Use func< (The < T, tresult>) >) delegate in C # with anonymous methods.

In 3.0, we can use the lambda to more clearly pass the parameters:
public class Example
{
public static void Demo (System.Windows.Controls.TextBlock outputblock)
{
Func<string, string> convert = S => s.toupper ();

String name = "Dakota";
Outputblock.text + = CONVERT (name) + "\ n";
}
}

The underlying type of a LAMBDA expression is one of the generic Func delegates. This allows a lambda expression to be passed as an argument without explicitly assigning it to a delegate. In particular, because many type methods in the System.Linq namespace have func< (of < (T, tresult>) >) parameters, you can pass lambda expressions to these methods without explicitly instantiating func< (of & lt; (T, tresult>) >) delegate. This will make our code more concise and logically easier to understand.

Initialization of an object

In C #, the initialization of the object also makes some improvements. A new function is to provide a more convenient syntax for declaring the value of a variable.

If we declare a student object:

public class Student
{
private string _stuname;
private string _stuage;
private int _stuclass;

Public Student () {}

public string Stuname
{
get {return _stuname;}
set {_stuname = value;}
}

public string Stuage
{
get {return _stuage;}
set {_stuage = value;}
}

public int Stuclass
{
get {return _stuclass;}
set {_stuclass = value;}
}

}

In c#2.0, we declare variables and assign values like this:

Student stu = new Student ();
Stu. Stuname = "Brian";
Stu. Stuage = "21";
Stu. Stuclass = "Class 1";

In c#3.0, we can initialize the object like this:

Student STU2 = new Student
{
Stuname = "Brian",
Stuage = "21",
Stuclass = "Class 1"
};

It is not hard to see from the code that C#3.0 provides us with a convenient way to initialize the object.

Inquire

This must have been heard by everyone, that is the famous LINQ. This is one of the most unique and useful new features in c#3.0. LINQ has changed the way we write data applications, and developers need to consider and write code that is not used to work with data from different data sources (SQL Server, XML, Memory ...). )。 LINQ is a good way to help us solve this annoying problem. With the help of lambda, we can more easily and accurately query the data we want.

Use LINQ for simple data query examples:

private void Bindgridview (string criteria)
{
String strconn = configurationmanager.connectionstrings["ConnStr"]. ConnectionString;
NORTHWINDDB db = new Northwinddb (strconn);

ienumerable<employee> results;

if (criteria = = String. Empty)
{
Results=db. Employee.toarray ();
}
Else
{
Results = (from C in DB. Employee
where C.firstname.contains (criteria)
Select C). ToArray ();

}
Gridview1.datasource = results;
Gridview1.databind ();
}

Variable declaration

Here's what we're going to say about var. var is a keyword that is provided in c#3.0 to declare variables, and developers can declare variables without regard to the type of the variable (which is very similar to JavaScript). But there are some differences between the two.

Same point: Use VAR to declare any type of local variable.

Different points: It is only responsible for telling the compiler that the variable needs to infer the type of the variable based on the initialization expression and can only be a local variable.

We can declare variables like this:

var i= 10;

var name = "Edisundong";

var numbers = new int[] {1, 2, 3};

var is just a keyword, it is not a new type in c#3.0, but is responsible for telling the compiler that the variable needs to infer the type of the variable based on the initialization expression, which is equivalent to

int i= 10;

String name = "Edisundong";

int[] numbers = new int[] {1, 2, 3};

Here are a few things to note:

1. You must assign values at the same time when declaring.

2. After using VAR to declare a local variable, he still has a strong type.

var integer = 10;

Integer = "Edisundong";

The cannot implicitly convert type string to int error is reported at compile time.

3. The compile-time type of the initializer expression cannot be a null (NULL) type.

4. var declaration is limited to local variables

Extension methods

In the past, if we wanted to extend the functionality of a class that had to come directly from it and learn from its methods, in c#3.0, a quick way to extend functionality was introduced.

public static Class Studentextensionmethods
{
Public Studentextensionmethods ()
{
//
TODO: Add constructor logic here
//
}
public static string Getstudentinformation (this Student stu)
{
return string. Format (' Name: {0} {1} ' age: {2} ', Stu. Stuname,
Stu. Stuage, Stu. Stuclass);
}
}


Defines a class that defines a method, noting that this class and method are static, and that the parameters of the method are class student. This allows the student class to extend the Getstudentinformation method:

Student STU2 = new Student
{
Stuname = "Brian",
Stuage = "12",
Stuclass = "Class 1"
};
Console.WriteLine (STU2. Getpersoninformation ());

Summary: c#3.0, feeling brings a lot of surprises, of which there are many new features are previously unknown. The new features of c#3.0 should be more than that, and further study is needed.

Http://developer.51cto.com/art/200901/107450.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.