using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics;
namespace Csharp { class People { private int myInt; public int MyIntProp//这是什么玩意 { get { return myInt; } set { myInt = value; } } } class Program { static void Main(string[] args) { People p = new People(); p.MyIntProp = 1; Console.ReadKey(); }
private int myInt; public int MyIntProp { get { return myInt; } set { myInt = value; } }
========》简写成
public int MyIntProp { get;set; } [解决办法]
private int myInt; public int MyIntProp { get { return myInt; } set {
myInt = value; } }
这种写法就是脱裤子放屁。 [解决办法] 相当于
private int myInt; public int Get_myInt() { return myInt; } public void Set_myInt(int value) { myInt = value; }
当myInt与接口无关时,其实就是
public int myInt;
[解决办法]
按照面向对象的做法,一个对象不应该有任何共有字段。 也就是一个对象只能通过它自己的方法去修改自身状态。 比如 class People { public int Age; } 这是不好的设计 你应该设计成 class Prople { private int age; public int Get_Age() { return age; } public void Set_Age(int value) { age = value; } } 为此,C#提供了简略的写法 class Prople { private int age; public int Age { get { return age; } set { age = value; } } } 也就是 class Prople { public int Age { get; set; } } [解决办法] 还有一种写法