如何为自定义类提交值

时间:2016-04-05 09:31:39

标签: c# .net oop

我有,

        public static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");

        //var queryList = Test.Join<Bar, Foo>(b => q => q.id == b.id);


        var queryList =  Test.NewJoin<Bar, Foo, int>(q => q.id, b => b.id);

        foreach (var telement in queryList)
        {
            var bar = telement.Inner as Bar;
            var element = telement.Outer as Foo;
            Console.WriteLine(element.id.ToString() + " " + element.FromDate.ToShortDateString() +" "
                              +bar.id.ToString() + " " + bar.Name
                             );
        }

        Console.Write("Press any key to continue . . . ");
        Console.ReadKey(true);
    }

现在创建一个demo类的对象,

public class demo
{
   public custom[] arr {get;set;}  //custom[] custom type of arr
}

public class custom
{
   public string x {get;set;}
   public string y {get;set;} 
}

是否可以使用demo obj=new demo(); obj.arr[0].x ="nyks"; // no error at compile time. run time exception. obj.arr[0].y="str"; 的实例在x,y中分配值? 如果是,怎么样?

1 个答案:

答案 0 :(得分:1)

您必须先初始化数组/对象才能使用它们

demo obj = new demo(); // this is wrong in your code, must be new demo(); 
obj.arr = new custom[1]; // create a new array with 1 customer
obj.arr[0] = new custom(); // fill the array with a new object customer
obj.arr[0].x = "nyks";
obj.arr[0].y = "str";