C #3.0 new features,

Source: Internet
Author: User

C #3.0 new features,

1. Automatic attributes

The following two statements share the same purpose

public int Age { get; set;}

  

private int Age;public int Age{  get { return age; }  set { age = value; }}

// The difference between the above two statements is equivalent to that between China and the People's Republic of China.

2. Empty type

For the value type, the specific value must be defined at the same time; otherwise, compilation may fail. however, in some cases, users do not know the value in advance, for example, they cannot know the student's age in advance. it is important to define an age as a type that can be null.

  • Common properties that can be empty:
  •  

    3. Generic Type

    4. Object Initiator

    Example:

    Student stu = new Student () {Name = "Zhang San", Age = 18 };

      

    5. Set Initiator

    Example:

    List <Student> stuList = new List <Student> () {stu,
    New Student () {Name = "", Age = 20}
    };

      

    6. var keywords

    Note 1: var B; this method is incorrect because the variable declared by var must be initialized at the same time as the definition.

    NOTE 2: Once a variable declared through var is instantiated, its data type cannot be changed. For example, the following code:

    Var a = 5; a = "zhangsan"; // a = "zhangsan" error

    7. Anonymous type

    When instantiating a class object, you can dynamically create class objects through new {} without having to define the class in advance.

    Example:

    Var preson = new {Name = "Wang Wu", Sex = "male "};

     

    8. Expansion Method

    Extends other methods for previously defined classes.

    Example: Student Extension Method

    Public static class StudentExtention {public static string ConsoleStudent (this Student stu) {if (! Stu. age. hasValue) {stu. age = 18;} string str = "name:" + stu. name + "\ n" + "Gender:" + stu. sex + "\ n" + "Age:" + stu. age; return str ;}}

      

    9. Anonymous type and Lambda expression

    Example:

    The usage is very complicated. We can use the anonymous method to abbreviated the above functions. Lambda expressions are also a quick way to write anonymous methods.

    Lambda expressions are divided into three parts: parameters |=> | expressions

    The following code demonstrates an example of using a Lambda expression to rewrite a delegate.

     

    Note 1: If the Lambda expression has only one parameter, the parameter does not need to be enclosed ().

    Note 2: The Lambda expression can be wrapped in {} or not. If there are multiple lines of code, you must wrap it in.

    Therefore, where the delegate (Func parameter) occurs, we can write Lambda expressions.

     

    Func <T, K> is a generic delegate. The parameter type of this delegate is T, and the return value type is K.

    The arg type of the Lambda expression parameter is T, and the partial return value type of the Lambda expression is K.

    10. Division

    Keyword: partial

    Partial is a class modifier used to split the class definition into several parts for easy code management.

     

    Ending ......

     

     

     


    In C language-> what?

    -> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
    For example:
    Struct Data
    {
    Int a, B, c;
    };/* Define struct */
    Struct Data * p;/* define struct pointer */
    Struct Data A = {1, 2, 3};/* declare variable */
    Int x;/* declare a variable x */
    P = & A;/* point p to */
    X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
    /* Because p points to A, p-> a = A. a, that is, 1 */

    For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
    Struct Data
    {
    Int;
    Struct Data * next;
    };/* Define struct */
    ............
    Main ()
    {
    Struct Data * p;/* declare the pointer Variable p */
    ......
    P = p-> next;/* assign the value in next to p */
    }
    The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
    What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
    What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
    Write so much. I hope you can understand.
    If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

    In C language-> what?

    -> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
    For example:
    Struct Data
    {
    Int a, B, c;
    };/* Define struct */
    Struct Data * p;/* define struct pointer */
    Struct Data A = {1, 2, 3};/* declare variable */
    Int x;/* declare a variable x */
    P = & A;/* point p to */
    X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
    /* Because p points to A, p-> a = A. a, that is, 1 */

    For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
    Struct Data
    {
    Int;
    Struct Data * next;
    };/* Define struct */
    ............
    Main ()
    {
    Struct Data * p;/* declare the pointer Variable p */
    ......
    P = p-> next;/* assign the value in next to p */
    }
    The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
    What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
    What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
    Write so much. I hope you can understand.
    If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

    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.