Since the release of the Microsoft March CTP version of Visual Studio and the. NET Framework "Orcas", many bloggers have reviewed and commented on the new features of this version in Run-time macros and code metrics.
Recently, the head of Microsoft ASP.net and Ajax development department Scott Guthrie also published some new features of C # 3.0 in his blog. The new features of the c#3.0 highlighted in the article are as follows:
Automatic properties--Automatic Properties
The Orcas version of C # can automatically establish private domains and default Get/set functions without the user having to manually declare private domains and manually establish Get/set functions. For example, when we are programming using the previous C # language, we need to write the following code snippet manually:
The following is a reference fragment:
public class Person ...{
private string _firstName;
private string _lastName;
private int _age;
public string FirstName ...{
get ...{
return _firstName;
}set ...{
_firstName = value;
}
}
public string LastName ...{
get ...{
return _lastName;
}
set ...{
_lastName = value;
}
}
public int Age ...{
get ...{
return _age;
}
set ...{
_age = value;
}
}
}
Using the Orcas version of C # we can rewrite the code as follows with its automatic property functionality:
The following is a reference fragment:
public class Person ...{
public string FirstName ...{
get; set;
}
public string LastName ...{
get; set;
}
public int Age ...{
get; set;
}
}
or in order to be more concise, we can reduce the blank area, the code is compact as follows:
The following is a reference fragment:
public class Person ...{
public string FirstName ...{
get; set;
}
public string LastName ...{
get; set;
}
public int Age ...{
get; set;
}
}
When the C # "Orcas" compiler encounters a blank Get/set property implementation as shown above, it automatically generates a private domain in your class while implementing a public getter and setter property implementation. The advantage of this is that, from a type-contract perspective, the class seems to use the execution (implementation) in the first paragraph of the code above. This means that, unlike the public domain, we can in the future add validation logic to the property setter implementation without having to change any external components related to the class.