0
 public static void Main()
    {// do something here
    String[] array1d = { "zero", "one", "two", "three" };
        ShowArrayInfo(array1d);
        PrintValues(array1d);
     //do something here
    }
 public static void ShowArrayInfo(Array arr)
    {//do something here}
 public static void PrintValues( string[] myArr )
    {//do something here}      

In above code there are two different functions called
1. ShowArrayInfo
2. PrintValues
but the way array is collected in both the functions are different. In one, it is Array arr and in another, it is int[] myArr.

What is the difference between these two ways of collecting array , is arr and myArr are alike? Is arr and myArr reference to original array : array1d ? Can we perform same operations on myArr as what we can perform on arr ?

Pranita Jain
  • 63
  • 1
  • 7
  • Array arr is an array of anything, whereas int[] myArr is specifically an array of ints. – Kevin Jun 30 '16 at 11:59
  • Default passing convention in C# is to pass as value, which means the method actually gets a copy of the array. To pass the original array you have to declare the passing convention as ref (by reference). – Kevin Jun 30 '16 at 12:01
  • `PrintValues( int[] myArr )` accepts Int type array and your passing it String type array is right?? – Jaydip Jadhav Jun 30 '16 at 12:02
  • @Kevin that isn't true. The *reference* is passed by value, but it's still a reference to the same array. – Charles Mager Jun 30 '16 at 12:03
  • Consider a more general approach and a method taking `IEnumerable` – w.b Jun 30 '16 at 12:05
  • thanks kevin for answer – Pranita Jain Jun 30 '16 at 12:05
  • @CharlesMager You're right I should have said "To pass a reference to the original array". – Kevin Jun 30 '16 at 12:11
  • @JaydipJ ya i was wrong on that , just edited.it is type string only. – Pranita Jain Jun 30 '16 at 12:13
  • @CharlesMager i saw you pointing about ref keyword using for array, Can i ask you something about this.as array are reference type so when we pass them into function then all changes performed inside array are reflected onto original array , then what can be the scenario where we still need to pass array by ref ? – Pranita Jain Jun 30 '16 at 12:56
  • @PranitaJain If you passed using `ref` then if you set `arr = new string[0]` then it would change `array1d` to that new array too. See [this related question](http://stackoverflow.com/questions/186891/why-use-the-ref-keyword-when-passing-an-object). – Charles Mager Jun 30 '16 at 13:02

3 Answers3

1

Arrays are Reference types, if you change elements in an array inside the function, this is reflected in the caller.

Array is like a more generic class for dealing with arrays. check the example below.

Using Array class, arr.GetValue(0) return object and not int.

static void ChangeFirstItemToTen(int[] arr)
{
    arr[0] = 10;
}

static void ChangeFirstItemToTen(Array arr)
{
    arr.SetValue(10, 0);
}

static void Main(string[] args)
{
    int[] values = new int[] { 5, 6, 7, 8 };
    ChangeFirstItemToTen(values);
    Console.WriteLine(values[0]); // prints 10;
}
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
0

Per the documentation, Array is the base class for all arrays in the CLR.

You can also see from the documentation that there are far fewer methods and properties available on the base class. There aren't many reasons you'd reference an array using Array instead of T[] (int[], in this case).

In your example, both arr and myArr are references to the same array as array1d. The main difference is the methods and properties available to you (though you could easily cast from one to the other).

Charles Mager
  • 25,735
  • 2
  • 35
  • 45
  • as you mentioned "though you could easily cast from one to the other" , so how to cast arr so that i can edit element of array by index like : arr[0] ="hello" – Pranita Jain Jun 30 '16 at 12:18
  • `var x = (int[])arr` and then you can use `x[0] = 10` or similar. This *will fail* if the type of underlying array is not `int[]`, however. – Charles Mager Jun 30 '16 at 12:20
0

Array is the base class for all arrays in C#. The only difference in the two methods defined by you is one will accept array of any type and other of type int only. Both will have the 'pass by value' technique.
You can use 'pass by reference' by using the ref keyword.

Sujeet Sinha
  • 2,417
  • 2
  • 18
  • 27
  • yes, i have gone through class Array metadata which typically states "Provides methods for creating, manipulating, ..." but here i'm not directly able to manipulate the array like arr[1] = "someValue" , well array class itself is not allowing such flexibility whereupon in my example i can easily do manipulation on myArr. M I correct if i say that since array class is base class and and arr can be anything like int or string array, 1d or 2d,so for safe side arr is not manipulable like myarr in that much flexible way, though array class provides general functions like copy,lenght,rank etc.. – Pranita Jain Jun 30 '16 at 12:29