-2
刚开始使用类。我的int age
不适用于我的属性Age。它始终为0。从未分配的c#字段的默认值为0
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Stuffers s = new Stuffers("Stuffy McStuff");
}
}
class Stuffers
{
private int age;
public int Age
{
get { return age; } set { value = age; }
}
}
}
显示错误:
field age never assigned to, and will have default value of 0
原非MCVE代码
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Stuffers s = new Stuffers("Stuffy McStuff");
Console.WriteLine(s.GetName());
Console.Write("{0}, what is your age?", s.GetName());
s.Age = Convert.ToInt32(Console.ReadLine());
Stuffers s2 = new Stuffers("Leeroy");
Console.WriteLine(s.GetAge());
}
}
class Stuffers
{
private int age;
private string name;
public int Age
{
get { return age; } set { value = age; }
}
public Stuffers(string nameC)
{
name = nameC;
}
public string GetName()
{
return name;
}
public int GetAge()
{
return age;
}
}
}
嗨特拉维斯,谢谢你的提问。不幸的是,stackoverflow社区不喜欢回答作业问题(我不是那个投了btw = P的人)。只是想我会让你知道的。 – Carlo
@卡罗你怎么知道这是作业?至少对我来说似乎不是这样。 – Sweeper
这不是家庭作业大声笑我只是自己学习和尝试的东西。感谢您的警告,虽然大声笑 –