直接帖代码:
public class Bird { int xdata; /// <summary> /// 属性的简洁写法,等同于下面的xData方式 /// </summary> public int Data { set; get; } /// <summary> /// 属性的正常写法 /// </summary> public int xData { set { xdata = value; } get { return xdata; } } /// <summary> /// 属性的递归 /// </summary> public int Type { set { if (value < 15) { Type = value + 1; //这里会递归调用set Console.WriteLine("value={0}", value); } } } } class Program { static void Main(string[] args) { Bird bd = new Bird(); bd.Data = 10; Console.WriteLine("data = {0}", bd.Data); bd.Type = 10; } }
运行结果: