SoFunction
Updated on 2025-03-03

.net get set usage summary page 1/3

In the early learning of C#, I often encountered such statements:
public string StudentName
{
           get{return stuName;}
           set{stuName=value;}
}
I didn’t understand why this was the case at that time? After learning C#, I couldn't make a summary of it. Today I read the book "Visual c#.net Programming Tutorial" and the summary is good. I took notes:

In many object-oriented programming languages, property refers to the features and state of an object, specifically the data members of the object. Programmers can specify whether the data member can be directly accessed by the outside world. If the data member is designated public, the outside world can use the "object name.public data member name" to access the member. C# is a completely object-oriented language. C# advocates a new approach that can better encapsulate and protect data members while providing more effective forms of access to the outside world. What is used to achieve this goal in C# is "properties", and those data members are called "fields" or "domains" in C#.

Definition and use of attributes
An attribute consists of two parts: the attribute header and the memory. Memory is divided into get accessor and set accessor. The general form of declared attributes is:
Modifier Type Attribute Name
{
get   //set access program
     {...}
set    //set access program
     {...}
}
The modifier of the attribute can be any access control character, and can also be defined as static. Get and set are a specific method. Get is used to read data from objects, and set is used to write data to fields. When writing external data to fields, c# uses value to represent the input data. Value can be said to be a quasi-keyword, for example:
set{aField=value;}
123Next pageRead the full text