我最近开始尝试自学 C#,这在很大程度上是初学者尝试在属性中使用业务规则的尝试,在我的例子中是 FurColor。当我运行下面的程序时,我得到一个NullReferenceException
. 有人可以帮我找出这个错误的原因吗?异常发生在第 15 行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _10_23_Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("programming practice!");
Dog d = new Dog();
Console.Write("what color is your dog: ");
d.FurColor = Console.ReadLine();
Console.WriteLine("the color of your dog is {0}", d.FurColor);
}
}
class Dog
{
private string furColor;
private string petName;
private int tagNum;
public Dog() { }
public Dog(string color, string name, int tagID)
{
furColor = color;
petName = name;
tagNum = tagID;
}
//properties
public string FurColor
{
get { return furColor; }
set {
do
{
Console.Write("enter in a viable color type: ");
}
while (furColor.Length > 10);
furColor = value;
}
}
public string Name
{
get { return petName; }
set { petName = value; }
}
public int TagNum
{
get { return tagNum; }
set { tagNum = value; }
}
}
}