获取,设置,不返回大于运算符的任何内容



我只是在测试一些东西,似乎它没有按预期工作。我看不清为什么这行不通。

现在看,添加了应该工作的整个程序??:

class Program
{
static void Main(string[] args)
{
AgeGroup a = new AgeGroup();
Console.Write("Write your age: ");
a.AgeTest1 = Convert.ToInt32(Console.ReadLine());
Console.ReadKey();
}
}
public class AgeGroup
{
private int age;
public int AgeTest1
{
get { return this.age; }
set
{
if (this.age > 65) 
Console.WriteLine("Nope not working!");
this.age = value;
}

}
}

}

您正在测试支持字段的当前值,而不是分配给属性的值。我假设你的意思是:

public int AgeTest1
{
get { return this.age; }
set
{
if (value > 65)
Console.WriteLine("Nope not working!");
this.age = value;
}
}

public int AgeTest1
{
get { return this.age; }
set
{
this.age = value;
if (this.age > 65)
Console.WriteLine("Nope not working!");
}
}

相关内容

最新更新