0

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

        }
    }
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 1
    Your array will be full of null elements. You need to instantiate the object in int[0] – Adam Houldsworth Dec 12 '13 at 07:55
  • 1
    A suggestion unrelated to your question: You might consider calling you class "item" rather than "items". This would be better because the class appears to model a single item rather than a collection of items. A small point, but it'll make your code more readable to anyone who isn't intimately familiar with it. – razlebe Dec 12 '13 at 08:03

4 Answers4

3

Array will be filled with default values after creating. MSDN:

If you do not initialize an array at the time of declaration, the array members are automatically initialized to the default initial value for the array type.

For reference types (i.e. classes) default values are nulls. So you should initialize array items before accessing them:

var i =new items[4];
// i[0] here is null

for (int k = 0; k < 4; k++) {
     i[k] = new items(r.Next()); // now i[0] points to object in memory
}

i[0].ID = 6;// now you can set object's property
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
1

Your item at index 0 is null. You need to do this instead

i[0] = new items{ID = 6};
Alex
  • 1,110
  • 8
  • 15
0

i[0].ID=6;// this line is kind of c++ code but how I do it in c#

Your array at tha point is empty. You initialize it after in loop.

So

  • first initialize array with values
  • after access/changes properties of the instances inside that array

    static void Main(string[] args)
    {
    
        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);
        }
    
        var i= items[4];
        i.ID=6;// NOW YOU CAN ACCCESS IT
    
        //OR SIMPLY 
        items[4].ID=6;
    
    }
    
Tigran
  • 61,654
  • 8
  • 86
  • 123
0

IN c++ when we declare the array of an object it is automatically initialize to NULL or "0" and we can get or set the value just after that(correct me if i am wrong). What about the c#? What i think is that var i=new items[4]; this line initializes the array as well,does it?