1

I have,

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;} 
}

Now create an object of demo class,

demo obj=new demo();

obj.arr[0].x ="nyks";  // no error at compile time.  run time exception.
obj.arr[0].y="str";

Is it possible to assign value in x,y using an instance of demo? If yes, how ?

leppie
  • 115,091
  • 17
  • 196
  • 297
nyks
  • 2,103
  • 6
  • 14
  • 17

1 Answers1

1

you have to initialize an array / objects before you can use them

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";
fubo
  • 44,811
  • 17
  • 103
  • 137