C#中关于get和set的详细例子介绍


Posted on September 27, 2008


get和set是用来扩展域的,也就是C里的变量,只是用起来更加灵活而己。看以下代码: 

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class test
    {
        public test()
        {
        }
        public string name2;
        public string name
        {
            get { Console.WriteLine("get method - name2.value" + name2); return "121212"; }
            set { Console.WriteLine("set method"); name2 = value; }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            test a = new test();
            a.name = "jmsupersar";
            Console.WriteLine(a.name);
        }
    }
}
 

标签:N/A

 

在线学习答案查询入口
微信扫一扫
微信扫码联系