【发布时间】:2015-10-25 00:32:06
【问题描述】:
您好,我正在 C# 中使用返回对象的嵌套属性进行一些测试,但我遇到了对象引用异常。
我希望能够访问嵌套属性中的数组,但在当前上下文中,我可以看到我没有在属性中实例化任何新对象。
这就是基本问题出现的地方...在这一切的中间我应该在哪里声明一个“新”对象实例?我什至需要在“foo”类或“bar”类中声明和新的对象引用吗?
namespace CustomProperties_TEST
{
class Program
{
public foo[] Blatherskite { get; set; }
static void Main(string[] args)
{
Program myProgram = new Program();
myProgram.Blatherskite[0].CustomProperty1[0].CustomProperty2 = 999999999;
myProgram.Blatherskite[1].CustomProperty1[0].CustomProperty2 = 999999999;
foreach (var item in myProgram.Blatherskite)
{
Console.WriteLine(item.CustomProperty1[0].CustomProperty2);
}
}
}
class foo
{
private bar[] customevariable1;
public bar[] CustomProperty1
{
get { return customevariable1; }
set { customevariable1 = value; }
}
}
class bar
{
private int customintvariable2;
public int CustomProperty2
{
get { return customintvariable2; }
set { customintvariable2 = value; }
}
}
}
【问题讨论】:
标签: c# object properties nested