C #3.0 new feature-automatically generate attributes

Source: Internet
Author: User

C # Since its birth, we have taken the lead of the family, and have thousands of pets in one, and are still famous . Net framwork A member of the family is naturally noble. Big brother in programming Java After more than a decade of development, it has been robust and powerful. Open Source Community Of SSH ( Struts + spring + hibernate ) Framework to make J2EE With unparalleled charm. Versatility, portability, and scalability are well supported. Every time we learn SSH I will give you a general idea later. Why? . Net There is no such excellent framework. Therefore. Net Fans also developed their own Sh ( Nspring + nhib.pdf ). But the two frameworks and . Net The programming style is always out of place. This is just something copied. But we do not need to be too pessimistic, C #3.0 The emergence of this feature has brought us a lot of new features. As for what new features, I will introduce them one by one.
First new feature: automatically generate attributes
Generally, we need to writeGET/SetTo provide external access and operations for attributes. For example:

 

 Code 
Public Class Userinfo
{
String Username;
String Password;
Public Username
{
Get { Return Username ;}
Set {Username = Value ;}
}
Public Password
{
Get { Return Password ;}
Set { Return Password = Value ;}
}
}

 

We always envyEclipseIsJavaProperty is automatically generatedGeter/seterMethods, althoughVisual StudioIt can also be similar, but it is not convenient to use, especially when a class has many attributes, write theseGET/SetThe method is really annoying. WhileC #3.0Finally let us get rid of this nightmare, and you will find that it is a feature of the language itself, comparedJavaOfGeter/seterThe method should be simpler, simpler, and more natural. Microsoft has made a lot of effort in terms of details.
Okay. Let's see how to use it.C #3.0To compile the above class.

 

 Code  
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> Public class userinfo
{< br> Public string username { Get ; set ;}< br> Public string password { Get ; set ;}< BR >}< br>

We can find that this is too easy. We only need to addGET/SetThe two keywords are used to write a few rows.Code.. Net framworkIt will automatically generate its private attributes for us. Using it is the same as using the above class.

 

Code
Userinfo=NewUserinfo ();
Userinfo. Username="Test ";
Userinfo. Password="Test ";

Now, we canJavaFans show off. Haha, just kidding.

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.