Get and set usage in Asp.net

Source: Internet
Author: User

When I learned C # in the early stage, I often encountered the following statements:

Public String studentname <br/>{< br/> get {return stuname ;}< br/> set {stuname = value ;}< br/>}< br/>

I didn't quite understand why? After learning C #, I cannot make a summary of it. Today I read Visual C #. netProgramThe design tutorial is a good summary. Take the following notes:

In many object-oriented programming languages, attributes refer to the features and states of objects, specifically the data members of objects. A programmer can specify whether a data member can be directly accessed by the outside world. If the data member is specified as public, the outside world can access the member by using "Object Name. Public Data member name. C # is a fully object-oriented language. C # advocates a new way to better encapsulate and protect data members and provide more effective access forms to the outside world. In C #, "attributes" are used to achieve this goal, and those data members are called "fields" or "fields" in C ".

Attribute definition and usage

An Attribute consists of an attribute header and a memory. Memory is divided into get accessors and set accessors. The general form of declared attributes is:

Modifier type attribute name

Public String propertyname <br/>{< br/> Get // get accessors <br/> {...} <br/> set // set accessors <br/> {...} <br/>}

The modifier of an attribute can be any access controller (public/private/protected), and can also be defined as static ). Get and set are specific methods. Get is used to read data from objects, while set is used to write data to fields. When external data is written to fields, C # Use value to represent the input data. value can be a quasi-Keyword, for example:

Set {afield = value ;}

The following is a simple example to demonstrate the basic form and usage of attributes:

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace get and set usage <br/>{< br/> public class student // student class <br/>{< br/> // The following three variable struname, stucollege and stuage are both data members in the attribute. <br/> private string stuname = "xuwei"; <br/> private string stucollege = "cugb "; // <br/> private int stuage = 24; <br/> Public String studentnmae <br/>{< br/>/* Get and set are specific methods, get is used to read data from an object, and set is used to write data to a field. <br/> * when writing external data to a field, C # uses value to indicate the input data, value is a quasi-Keyword, so you do not need to declare the value variable <br/> */<br/> get {return stuname ;} <br/> set {stuname = value ;}< br/>}< br/> Public String studentcollege // The school name cannot be changed, so no set accesser is available, set is used to modify the attributes of studentcollege <br/>{< br/> get {return stucollege ;}< br/> // set {stucollege = value ;} <br/>}< br/> Public int studentage <br/>{< br/> get {return stuage ;}< br/> set {stuage = value ;} <br/>}< br/> Public String studentinfo <br/> {<br/> get {return "name:" + studentnmae + "," + "school: "+ studentcollege +", "+" Age: "+ studentage ;} <br/>}</P> <p> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> Student Stu = new student (); <br/> console. writeline (Stu. studentnmae); <br/> Stu. studentnmae = "xuyanghong"; // you can assign a value directly. The value here is equivalent to the previous set {stuname = value ;}< br/> console. writeline (Stu. studentnmae); <br/> Stu. studentage = 25; <br/> Stu. studentnmae = "zhangchengrong"; <br/> console. writeline (Stu. studentinfo); <br/>}< br/>

AboveCodeDefines a property of studentname, which includes get accessors and set accessors. The attribute studentname encapsulates the stuname field in the student class. If the field is not added with an access control character, it is set to private by default and cannot be accessed directly by the outside world, now, the stuname field can be freely accessed through the studentname attribute.

The get and set attributes are a combination of executable program statements and have behavior characteristics. When using properties with get accessors and set accessors, they are like using fields, that is, data can be accepted as the left value and output as the right value. The system automatically selects whether to call get or set according to the position where the attribute appears in the statement.

Read/write control of attributes

Only one get and set can be used in the attribute. If only get is used but no set is used, this attribute can only be read and cannot be written. If only set is used and no get is used, this attribute is only written, not readable.

Complete more functions in properties

Since get and set are programs, you can certainly do more. A reasonable division of labor is: design fields are designed to facilitate the use of internal methods, and try to be isolated from the outside world; design attributes are designed to facilitate the use of the outside world, however, data that is not known to the outside world is not provided.

All in all: Using get and set can make attributes more encapsulated, and both on the left side of the equal sign and on the right side of the equal sign.
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.