I have been using the c++ and new in c#. In c++ i use int a[8]
to declare an array and if the array is of an object we set the value by item[0].SetID(5)
which will set the value of first item's ID to 5. But i m unable to do it in c#.
namespace Arrays
{
class items {
public int ID { set; get; }
public string name { set; get; }
public items(int ID) {
this.ID = ID;
name = "Faizan";
}
}
class Program
{
static void Main(string[] args)
{
var i=new items[4];
i[0].ID=6;// this line is kind of c++ code but how I do it in c#
Random r = new Random();
for (int k = 0; k < 4; k++) {
i[k] = new items(r.Next());
}
foreach(items it in i){
Console.WriteLine("The item name {0} and the Id is {1}",it.name,it.ID);
}
}
}
}