C # basic syntax Learning (3 ),

Source: Internet
Author: User

C # basic syntax Learning (3 ),

1. C # attributes: Use the following code to define an attribute:

1         private string name;2         public string Name3         {4             get { return name; }5             set { name = value; }6         }

Row 1st defines a variable, which is the basic variable on which the attribute depends. 2-6 lines of code define a Name attribute. Users of the class can read and modify the basic variables in the class through the Name attribute. For example:

1 Student student = new Student();2 3 student.Name = "Nick";

In the opinion of the property user, the property is like a variable with public access permission in the class, but this is not the case. The Name attribute defined in the previous Code actually defines two special methods: GetName and SetName, which are used to read and modify the private variable Name in the class respectively.

The 4th line of code is called the read method (getter) of the attribute ). The read method of the attribute must return the same type as the attribute. When a Code reads a property, the read method is called and the return value is handed over to the code that calls the property.

The 6th line of code is the property setting method (setter). The setting method accepts an entry parameter of the same type as the property type and specifies in C # That the entry parameter is named value, therefore, you can directly use the value variable without declaring it in the attribute setting method. When an attribute is assigned a value, the setter method is called.

The properties in C # generally have a get method and a set method. The get method is used to read attribute values. The set method is used to set attribute values, but not all are required, only the get method or set method can be used for attributes. Only the properties of the get method can be read. Only the properties of the set method can be written and cannot be read.

Benefits of using attributes: hiding data members improves program robustness and flexibility.

Attribute verification data can be used to verify the data by using attributes, so as to avoid accepting invalid data and improve program robustness. In the following example, the attribute is used to limit the age value.

 1     class Student 2     { 3         private string name; 4         public string Name 5         { 6             get { return name; } 7             set { name = value; } 8         } 9 10         private int age;11         public int Age12         {13             get { return age; }14             set 15             {16                 if (value < 0 || value > 120)17                 {18                     Console.WriteLine("Invaild age!!");19 20                     return;21                 }22                 else23                 {24                     age = value;25 26                     Console.WriteLine("Age set to: {0}", age);27                 }28             }29         }30     }31 32         static void Main(string[] args)33         {34             Student student = new Student();35 36             Console.WriteLine("Set student name : Nick");37             student.Name = "Nick";38             Console.WriteLine("Set student age: 25");39             student.Age = 25;40             Console.WriteLine("Student " + student.Name + "'s age is {0}!", student.Age);41 42             Console.WriteLine("Set student age: 140");43             student.Age = 140;44 45             Console.WriteLine("Student " + student.Name + "'s age is {0}!", student.Age);46 47             Console.WriteLine("Press enter to exit!");48             Console.ReadLine();49         }

The running result is as follows:

  

1 Set student name : Nick2 Set student age: 253 Age set to: 254 Student Nick's age is 25!5 Set student age: 1406 Invaild age!!7 Student Nick's age is 25!8 Press enter to exit!

 

Attribute is actually a special method, not a variable. Sometimes attributes are indeed encapsulation of member variables in the class, But this is often not the case.

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.