这咋用,干啥用的
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();
} }
}
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();
} }
}
public int MyIntProp
{
get
{
return myInt;
}
set
{
myInt = value;
}
}========》简写成public int MyIntProp { get;set; }
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; }
}
public int myInt { get; set; }下面这种写法还有点意义
public int myInt { get; private set; }
在C# 中,规范了属性用大写,字段用小写,如:
private int age -> 字段。
public Age{set{this.age = value};get{return this.age}}
这些都是C# 内置的工作,不明白的话,可以看 Reflector ···
这是平时写的,都省略了!
就如, 对象.Age = "Lisi" ->内部调用 Age{set{this.age = value}
int output = 对象.Age ->内部调用 get{return this.age}