C#:我正在尝试使用其他一些用户定义的类的列表创建集合类,但是当我尝试在主对象中添加对象时,它显示对象未设置为对象的实例。这是我的 C# 代码:
namespace ConsoleApplication2
{
public class Monthlst
{
public List<Monday> Monday { get; set; }
public List<Tuesday> Tuesday { get; set; }
public List<Wednesday> Wednesday { get; set; }
public List<Thursday> Thursday { get; set; }
public List<Friday> Friday { get; set; }
public List<Saturday> Saturday { get; set; }
public List<Sunday> Sunday { get; set; }
}
public class Monday
{
public int days { get; set; }
}
public class Tuesday
{
public int days { get; set; }
}
public class Wednesday
{
public int days { get; set; }
}
public class Thursday
{
public int days { get; set; }
}
public class Friday
{
public int days { get; set; }
}
public class Saturday
{
public int days { get; set; }
}
public class Sunday
{
public int days { get; set; }
}
class Program
{
static void Main(string[] args)
{
Monthlst objmonth = new Monthlst();
Wednesday wednes = new Wednesday();
wednes.days = 5;
objmonth.Wednesday.Add(wednes); // here i am getting error object not set to....
}
}
}
在这里,我只是在 main 方法中创建了我的 Monthlst 类的实例,并在 Monthlst 对象的 list 属性中创建了周三类的对象,但是为什么它向我显示错误我不知道我做错了什么,请解释....
谢谢,拉吉