1
Answer

Can we initilize multiple arrays through one indexer OR can we initilize multiple arrays through multiple indexers in one class?

Photo of Faisal Ansari

Faisal Ansari

13y
1.8k
1
hello how r u guys 

Q: Can we initlize more then one arrays through one indexer in c sharp OR can we initlize multiple arrays thourgh multiple indexers in c# ?

i am trying this code :
 public class Name
    {
        public string[] name = new string[4];

        public string this[int index]
        {
            get
            {
                return name[index];
            }
            set
            {
                name[index] = value;
            }
        }

        public int[] intArray = new int[4];
        public int this[int index]
        {
            get
            {
                return intArray[Convert.ToInt32(i)];
            }
            set
            {
                intArray[Convert.ToInt32(i)] = value;
            }
        }
    } 

Reply Soon 

Thanks.

Answers (1)

0
Photo of Vulpes
NA 98.3k 1.5m 13y
Well, that code won't compile as the 'signatures' of the two indexers are the same - i.e. they both take a single int parameter. The fact that the return type is different doesn't help because the return type of a method or indexer is not part of its signature.

Although you can overload indexers with different parameter numbers and/or types (we had a thread about this yesterday), I don't think this will help if you're trying to index multiple single dimensional arrays.

I'd just use the indexer for one of them (the name) and use an ordinary method for the other.

Accepted