C# 基础(八)C# get set 以及value 表示什么意思?举例说明

1、基础知识(你只有程序拷贝过去,本身单步执行,才能发现Value是怎样来的)html

一、只读spa

//https://www.cnblogs.com/lixiaolu/p/8214037.html3d

http://www.cnblogs.com/zl181015/p/9243881.htmlcode

二、只写htm

//https://www.cnblogs.com/lixiaolu/p/8214037.htmlblog

http://www.cnblogs.com/zl181015/p/9243881.htmlget

三、读、写string

附上代码:it

namespace test
{
    class Program
    {
        static int a3 = 3;
        static public int A3
        {
            get { return a3; }
            set { a3 = value; }
        }

        //string str3 = "aaa";
        //public string Str3
        //{
        //    get { return str3; }
        //    set { str3 = value; }
        //}
        static void Main(string[] args)
        {
            Console.WriteLine(a3);
            Console.WriteLine(A3);
            Console.WriteLine();

            A3 = 22;
            Console.WriteLine(a3);
            Console.WriteLine(A3);
            Console.ReadLine();
        }
    }
}

四、Value是什么意思?怎么使用呢?io

设置一个断点在A3,你会发现value的值就是等于22:

书本是这样说的:

 

固然,你也能够单部执行A4看看,你会发现 Value的值就是21(与A3同理):

步骤1:A4 = 44, a4 = 44,  Value 不存在

 

步骤2:A4 = 44, a4 = 44,  Value = 21(注意到这里没有,虽然给A4赋值了21,可是它并无当即更改。这里只有value改变为21

 

步骤3:A4 = 20, a4 = 20,  Value = 21(a4 = 20这一步执行完以后,A4 也变成了 20

步骤4:A4 = 20, a4 = 20,  Value 不存在

附上包含A三、A4的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class Program
    {
        static int a3 = 3;
        static public int A3
        {
            get { return a3; }
            set { a3 = value; }
        }

        static int a4 = 44;
        static public int A4
        {
            get { return a4; }
            set
            {
                if (value > 10)
                {
                    a4 = 20;
                }
                else if (value < 10)
                {
                    a4 = 5555555;
                }
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine(a3);
            Console.WriteLine(A3);
            Console.WriteLine();

            A3 = 22;
            Console.WriteLine(a3);
            Console.WriteLine(A3);

            Console.WriteLine();
            Console.WriteLine(a4);
            Console.WriteLine(A4);
            A4 = 21;
            Console.WriteLine(a4);
            Console.WriteLine(A4);
            Console.ReadLine();
        }
    }
}

 

总结:

一、A4与a4的值,老是相等。

二、若要修改a4,则须要经过A4修改便可。a4的每每经过设置外部属性A4值的方式,而后在A4属性内设置一些条件,来更新a4。

三、Value的值,老是等于外部属性A4的值。