請問為什麼我賦值給屬性的值是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;
}