-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
這不是家庭作業大聲笑我只是自己學習和嘗試的東西。感謝您的警告,雖然大聲笑 –