C # 3.0 Language Features

Source: Internet
Author: User

Keywords: implicit type variable var, extension method (extension), lambda expression, auto attribute, anonymous type

One, implicit type

C # is a strongly typed language, meaning that we must specify the exact type of the variable when declaring it, but Var can replace the formal data type name (such as int, bool, string), and the compiler infers the data type of the variable based on the initial value used to initialize the local variable We can use implicit types for all types in the base class library, including arrays, generics, custom types.

Usage Restrictions:

1. You can only apply the declaration of local variables within the method or attribute, and you cannot use Var to define the return value, the type of the parameter, or the data member of the type;

2. Local variables declared with VAR must be assigned an initial value, and cannot be null as the initial value.

Strongly typed data: After an implicit type initialization, the compiler has inferred the exact type, so he is strongly typed.

Role: You can dynamically create result sets based on the format of the query itself, so that we do not need to display the types that the definition query might return

Second, automatic attributes

Because. NET base class libraries always use type properties instead of traditional access and modification methods, so. NET language recommends using type attributes to encapsulate private data fields instead of using the getxxx () and Setxxx () methods.

At the bottom, C # properties are mapped to methods that prefix get_ and set_, meaning that C # automatically generates the Get_name () and Set_name () methods if the Name attribute is defined.

When defining automatic attributes, you must provide both the get and set keywords.

Iii. types of anonymity

A class can express a given programming entity, we typically create a C # class that provides the necessary set of properties, methods, and events for reuse, and sometimes you might need to define a class to encapsulate some related data without requiring any associated methods, events, or the class to be reused across projects.

C # 3.0 provides anonymous types, using the New keyword var and the object initialization syntax described earlier, such as: var worker = new {FirstName = "Vincent", LastName = "Ke", level = 2}; The C # compiler automatically generates classes with unique names at compile time. Because the name of this class is not visible in C #, it is necessary to use the VAR keyword to use implicit typing.

Anonymous type internal representation:

Anonymous types are automatically inherited from System.Object, and can be called by the ToString (), GetHashCode (), Equals (), GetType () methods

  

C # 3.0 Language Features

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.