请问为什么我赋值给属性的值是7,最后输出的还是4,属性set块没有修改字段的值?
public class Content
{
private int Val=4;
public int val
{ get
{
return Val;
}
set
{
value = Val;
}
}
}
class Program
{
static void Main(string[] args)
{
Content mySource = new Content();
mySource.val = 7;
Console.WriteLine(mySource.val);
Console.ReadKey();
}
}
set中的赋值对象搞错了,应该像下面:
public int val
{ get
{
return Val;
}
set
{
Val = value;
}