SoFunction
Updated on 2025-04-11

.net get set usage summary page 2/3


Here is a simple example that demonstrates the basic form and usage of properties:
using System;
using ;
using ; 
Usage of namespace attribute
{
    public class Student
    {
private string stuName = "A Huinan";
        public string studentName
        {
            get { return stuName; }
            set { stuName = value; }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student();
            ();
            ();
        }
    }
}

The above code defines a property studentName, which contains the get accessor and the 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. The outside world cannot access it directly. Now the outside world can freely access the stuName field through the studentName property.

The get and set of attributes are executable program statement combinations, with behavioral characteristics; when using properties with get accessors and set accessors, it is like using fields, that is, it can accept data as lvalues ​​and output data as rvalues. The system automatically chooses whether to call get or set according to the location where the attribute appears in the statement.
Previous page123Next pageRead the full text