我还换了这种写法,结果也是一样。谁指点指点啊,到底该怎样认识get访问器呢。 class Program { static void Main(string[] args) { thePlus tp = new thePlus(); PlusX(tp.X); } public static void PlusX(int value) { Console.WriteLine("Value is: {0}", value); Console.ReadKey(); } } class thePlus { private int x; public int X { get { return x++; } } }
这个和get访问器没关系。,你将 public static int X中的 static 去掉,,应该就是1了
x++先取值,再自加 只带有 get 访问器的属性称为只读属性。无法对只读属性赋值。 get 访问器都必须在属性体的内部声明。
楼主变成, get { return ++x; }
+1 class thePlus { private int x; public int X { get { return x++; } } }//效果相当于class thePlus { private int x; public int X { get { int t = x; x++; return t; } } }
{
static void Main(string[] args)
{
thePlus tp = new thePlus();
PlusX(tp.X);
} public static void PlusX(int value)
{
Console.WriteLine("Value is: {0}", value);
Console.ReadKey();
}
} class thePlus
{
private int x; public int X
{
get { return x++; }
}
}
static 去掉,,应该就是1了
只带有 get 访问器的属性称为只读属性。无法对只读属性赋值。
get 访问器都必须在属性体的内部声明。
+1
class thePlus
{
private int x;
public int X
{
get { return x++; }
}
}//效果相当于class thePlus
{
private int x;
public int X
{
get
{
int t = x;
x++;
return t;
}
}
}