7

Is there any difference between using myArray.GetValue(2) and myArray[2]?

For example:

namespace ConsoleApplication16
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = new int[] { 1, 2, 3, 4 };
            Console.WriteLine(numbers.GetValue(3));
            Console.WriteLine(numbers[3]);
            Console.ReadLine();
        }
    }
}
Vassalware
  • 30
  • 3
  • 8
Jakub Gause
  • 309
  • 3
  • 10
  • 2
    Not in the code you wrote. It's useful for non 0-based arrays and in languages without support for indexer (or for arrays...) – Adriano Repetti Apr 14 '17 at 14:36

5 Answers5

2

GetValue will return type object while using the index will return the type specific with the array.

You can see in this fiddle (code below) that the variable val1 can have a string stored in it, but val2 can only be used as an integer.

public static void Main()
{
    int[] numbers = new int[]{1, 2, 3, 4};
    var val1 = numbers.GetValue(3);
    var type = val1.GetType();
    var val2 = numbers[3];

    Console.WriteLine(type.ToString());
    val1 = "hello";
    type = val1.GetType();
    Console.WriteLine(type.ToString());
}

This will result in boxing and unboxing, which won't have an effect on a small code snippet, but if used in large scale it could potentially affect performance.

Smeegs
  • 9,151
  • 5
  • 42
  • 78
1

In your code, there isn't a difference. The primary difference, inside an array when you call Array.GetValue(1) you're receiving the value of that index. You can't specifically set the value of the array though.

If you were to do Array[1] = "..."; you've modified the array. But you can also still attain the value, though it may not be as expressive as above. So you can get or set a value with this approach.

The number of elements in indices must equal the number of dimensions in the Array. All elements in the indices array must collectively specify the position of the desired element in the multidimensional Array. The GetLowerBound and GetUpperBound methods can determine whether any of the indexes is out of bounds. This method is an O(1) operation.

Both are susceptible do the above though.

Greg
  • 11,302
  • 2
  • 48
  • 79
1

numbers.GetValue(3) returns an object and therefor the overload Console.WriteLine(object value) that takes an object parameter is called.

numbers[3] is of type int and then Console.WriteLine(int value) that takes an int as parameter is called.

So you end up calling different methods.

heijp06
  • 11,558
  • 1
  • 40
  • 60
0

In use-ability there isn't a difference except for setting values. The difference however seems to be in how it gets the value.

Array's .GetValue gives me this, it returns an object.

Either approach takes under 1ms so there isn't a substantial speed difference either.

EpicKip
  • 4,015
  • 1
  • 20
  • 37
0

When you are using Array class(with uppercase) there are not indexers. So the way you can get and set elements on it is using GetValue() or SetValue() See this link : Using an array's SetValue method vs. the [] indexers

Community
  • 1
  • 1
AlexGH
  • 2,735
  • 5
  • 43
  • 76