c#3.0 new features-automatic generation of attributes

Source: Internet
Author: User
Tags ssh visual studio

C # Since the birth of the home to take the long, set thousands of love in one, but also a noble family. NET Framwork is a member of the clan, is born noble. The programming world of Big Brother Java, after more than 10 years of development, has been robust and powerful. With the open source community's ssh (struts+ spring+hibernate) framework, the Java EE has unparalleled charm. Versatility, portability and scalability have been well supported. Every time we learn ssh, we get a sense of why. NET does not have such a good frame. So. NET fans have also developed their own sh (nspring+nhibernate). But these two frameworks and. NET programming style always seems to be out of tune, this is simply copied things. But we also need not be too pessimistic, the appearance of c#3.0 brought us many new features, as to what is the new features, I will give you a description.

First new feature: Automatically generate properties

Typically, we write Get/set methods for each class to provide access and operations to the property externally. Such as:

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 envy Eclipse. The functionality of the Geter/seter method is automatically generated for the Java properties, although Visual Studio can do similar functions, but it is not easy to use, especially when a class has a lot of properties, writing these get/ Set method is really a very annoying thing. And c#3.0 's automatic generation attribute, finally let us get rid of this nightmare, and you will find it is the language itself, more simple, simpler and more natural than the Java Geter/seter method. Microsoft has made a lot of effort in detail.

Well, let's see what it's like to write the above class using c#3.0 's automatic generation properties.

Code
public Class UserInfo
{
  public string UserName{get;set;}
  public string Password{get;set;}
}

We can see that this is really too easy. All we need to do is add the Get/set two keywords to the attribute and implement the previous method of writing a few lines of code. The. NET framwork automatically generates its private properties for us. Using it now is the same as using the class above.

Code
UserInfo userInfo=new UserInfo();
userInfo.UserName=”test”;
userInfo.Password=”test”;

Now, we can show it to the Java fans. Oh, a joke.

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.