The usage of get and set in asp.net

In the early learning of c#, I often encountered such statements:
public string StudentName
{ get{return stuName;} set{stuName=value;} } At that time, I didn’t really understand why it was so? After learning c# deeply, I can't make a summary of it. Today I read the book "Visual c#.net Programming Tutorial", and I summarized it well, and I made a note: In many object-oriented programming languages, properties refer to the characteristics and states of objects, specifically referring to The data member of the object. The programmer can specify whether the data member can be directly accessed by the outside world. If the data member is designated as public, the outside world can use "object name. public data member name" to access the member. C# is a completely object-oriented language. C# advocates a new way to better encapsulate and protect data members, while at the same time providing more effective forms of access to the outside world. C# is used to achieve this goal is "attribute", and those data members are called "fields" or "domains" in c#. Definition and use of attributes Attributes are composed of two parts: attribute header and memory. The memory is divided into get accessor and set accessor. The general form of declaring properties is: modifier type property name public string propertyName { get //get access program {…} set //set access program {…} }















The attribute modifier can be any access control character (public/private/protected), or it can be defined as static (static). Get and set are a specific method. Get is used to read data from the object, and set is used to write data to the field. When writing external data to the field, c# uses value to represent the input data, value can be said Is 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;
using System.Collections.Generic;
using System.Linq;
using System.Text ;

namespace get and set usage
{ public class Student//student class { //The following three variables StruName, StuCollege, and StuAge are all data members in the attribute private string StuName = "xuwei"; private string StuCollege = "CUGB";// private int StuAge = 24; public string studentNmae { /*get and set are a specific method, get is used to read data from the object, and set is used to write data to the field, * write external data to the field When, c# uses value to represent the input data, value can be said to be a quasi-keyword, so there is no need to declare the variable value */ get {return StuName;} set {StuName = value;} } public string studentCollege//School name is not available Change, so there is no set accessor, set is used to modify studentCollege attributes { get {return StuCollege;} // set {StuCollege = value;} } public int studentAge {





















get { return StuAge; }
set { StuAge = value; }
}
public string studentInfo
{
get { return “name:” + studentNmae + “,” + “school:” + studentCollege + “,” + “age:” + studentAge; }
}
}

class Program
{ static void Main(string[] args) { Student stu = new Student(); Console.WriteLine(stu.studentNmae); stu.studentNmae = "xuyanghong";//Can be assigned directly, the value here is equivalent The previous set {StuName = value;} Console.WriteLine(stu.studentNmae); stu.studentAge = 25; stu.studentNmae = "zhangchengrong"; Console.WriteLine(stu.studentInfo); } } }The above code defines one Attribute studentName, it contains get accessor and set accessor. The attribute studentName encapsulates the field stuName in the class Student. If the field is not added with an access control character, it is defaulted to private, and the outside world cannot directly access it. Now the outside world can freely access the stuName field through the studentName attribute.












Attribute get and set are both executable program statement combinations and have behavioral characteristics; while using attributes with get accessor and set accessor is just like using a field, that is, it can accept data as an lvalue and can be used as a right Value output data, the system automatically chooses whether to call get or set according to the position where the attribute appears in the statement.

Property read and write control

You can use only one of get and set in the attribute. If there is only get but no set, then this attribute can only be read but not write; if there is only set but no get, then this attribute is only write but not read.

Complete more functions in the properties

Since get and set are programs, of course you can do more. A reasonable division of labor is: the design field is to facilitate the use of internal methods, and try to isolate it from the outside world; the design attribute considers the convenience of the outside world, but the data that is not known to the outside world will not be given.

All in all: using get and set can make attributes have better encapsulation, and can be on the left side of the equal sign and on the right side of the equal sign.

Guess you like

Origin blog.csdn.net/qq_41661800/article/details/105243743