C #3.0 new language features and improvements (1 ),

Source: Internet
Author: User

C #3.0 new language features and improvements (1 ),
Introduction

As for the features of C #3.0, you may be familiar with it. Although I have used it in development, I still need to record it and summarize it. At the same time, it is also the basis for writing the knowledge of Linq later. If you are interested, you can check it out.

 C #3.0 new language features and improvements include:

  • Automatic attributes
  • Implicit type local variable
  • Anonymous type
  • Object and set Initiator
  • Extension Method
  • Lambda expressions and Lambda Expression Tree
1. Automatic attributes

Automatic attribute can avoid declaring private variables and the get/set logic when writing object classes. Instead, the compiler will automatically generate a private variable and default get/set operation for you.

In. Net2.0, we define a Product class as follows.

public class Prodcut    {        private string _productid;        public string Productid        {            get { return _productid; }            set { _productid = value; }        }        private string _prodcutname;        public string Prodcutname        {            get { return _prodcutname; }            set { _prodcutname = value; }        }    }

Automatic attribute writing:

    public class Prodcut    {        public string _productid { get; set; }        public string _prodcutname { get; set; }    }

If the get/set attribute is empty, a member variable and a public get/set will be automatically generated in the class. You can also set the access level of get/set separately, as shown below:

    public class Prodcut    {        public string _productid { get; private set; }        public string _prodcutname { private get; set; }    }
2. Implicit local variables

C #3.0 introduces the New Keyword var, which can be used to replace the original type name when declaring a local variable. Therefore, this declaration is considered as an implicit type local variable declaration.

Var I = 5; var j = "Hello World"; var k = 50.36; var x; // error var y = null; // error var z = {1, 2, 3}; // Error

Remove the error code. Check the data type in the debugging status.

Hidden type local variables:

  • Var local variables must be initialized.
  • The var keyword indicates that the compiler can infer the actual data type through the initialization part on the right.
  • The expression type cannot be null during compilation.
  • The initialization statement can be an expression, cannot be null, and can be compiled to determine the type. Once initialized, only this type can be stored.
  • Var can only declare local variables, not global. It can also be used in statements such as foreach, for, and Using.
  • Initialization cannot be an object or a set initiator, but it can contain a new expression of an object or an object.
3. Anonymous type

1. What is an anonymous type?

The anonymous type, as its name implies, is a type without a name (invisible to the surface, and the class name is automatically generated by the compiler.

2. What is the role of the anonymous type?

When you need to define a class to encapsulate some relevant data without any associated methods or events, and this class does not need to be reused in the project, we can also consider using anonymous types to simplify our operations.

3. Usage

 var User = new { Name = "swich", Url = "http://www.cnblogs.com/liujie2272" };

For the above anonymous type, the editor considers that the User is equivalent:

 public class User    {        public string Name { get; set; }        public string Url { get; set; }    }

Array anonymous type:

 var array = new[]   {         new { Name = "ZhangSan", Age = 26 },         new { Name = "LiSi", Age = 25 }  };

 

Key points of the anonymous type:

  • You can use the new keyword to call an anonymous object to create an anonymous object.
  • The anonymous type is inherited from System. Object.
  • The type of the anonymous type is the read/write attributes inferred by the compiler based on the initiattor.
4. Object and set Initiator

1. Object Initiator

In. Net 2.0, It is very dependent on attributes. When an object instance is generated and an object is used, we write it as follows:

 User user = new User(); user.Name = "swich"; user.Url = "http://www.cnblogs.com/liujie2272";

In. Net 3.0,The object initializer is composed of a series of member objects, whose objects must be initialized and closed with commas.We can write as follows:

User User = new User { Name = "swich", Url = "http://www.cnblogs.com/liujie2272" };
  List<User> list = new List<User>            {               new User { Name = "swich", Url = "http://www.cnblogs.com/liujie2272" },               new User { Name = "swich", Url = "http://www.cnblogs.com/liujie2272" }            };

2. Set Initiator

The Set initiator is composed of a series of set objects separated by commas (,) and closed.

List<int> sum= new List<int> { 11,21,31,41,51};

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.