c#4.5 new syntax-automatic attributes and implicit types

Source: Internet
Author: User

1. Automatic Attributes
automatic attributes are one of two forms of attribute definition in C #: traditional attribute definitions, automatic attributes.
1.1 traditional attribute definitions
private int _age;
public int age
        {
get {return _age;}
set {_age = value;}
        }
1.2 Automatic Properties
public int Age{get;set;} The function of this code is the same as the statement defined by the traditional attribute above .
1.3 Comparison of the pros and cons of two types of attribute definitions
1.3.1 Traditional attribute definition form
Cons: Code that defines attributes is not concise and cumbersome
Pros: When defining attributes, you can take some logical decisions, which are not possible with automatic attributes.
For example:
private int _age;
public int age
        {
get {return _age;}
set {
if ((0<value) && (value<200))
                {
_age = value;
                }
            }
        }
1.3.2 Automatic attribute definition form
Pros: Define a brief introduction to the syntax, efficiently and reduce the amount of code written by the programmer
Cons: When defining attributes, it is not possible to make some logical judgments.
2, the role of attributes
1. Define properties to protect private fields of a class (encapsulation)
2, simplifies the call to the class (after defining the property, to invoke the _age field, do not need to instantiate the class)

3. Implicit type var
3.1 Overview
There are two ways to define variables: the traditional definition, the implicit type.
Traditional definition: Data type + variable name (can be initialized when defined or uninitialized) int k=0;int k;
Implicit type definition: var+ variable name (must be initialized when defining) var k=0;
3.2 Note
(1). The declaration must be initialized at the same time, because the type of VAR is inferred from the initialization value type at compile time;
(2). The data type cannot be modified after the declaration, because the type of VAR has been determined at initialization time;
(3). Can only be used as a method local variable. The member field of the class, the parameter of the method, and the return value are not available! Because their type cannot be determined at compile time!
(4). An initialization expression cannot be an anonymous function and null.
(5), implicit type is not a dynamic type, when compiling, the. NET Framework k=0 var; convert to int k=0;

c#4.5 new syntax-automatic attributes and implicit types

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.