When you want to pass an array to a method, you could first declare the array and then pass the array by name to the method.
Suppose that you have a Dog method that looks like this:
1 2 3 4 5 | public void DoBarks(string[] barkSounds){ foreach (string s in barkSounds) Console.WriteLine(s);} |
You can declare the array and pass it to the method:
1 2 3 4 5 | Dog d = new Dog();// Declare array and then passstring[] set1 = { "Woof", "Rowf" };d.DoBarks(set1); |
Or you can just initialize a new array instance as part of the method call, without first declaring it:
1 2 | // Initialize and pass array without declaringd.DoBarks(new string[] { "Grr", "Ack-ack" }); |
You can also initialize a multi-dimensional array as part of a method call:
1 2 | // Initialize and pass multi-dimensional arrayd.AddNumbers(new int[,] { { 1, 2, 3 }, { 9, 10, 11 }, { 100, 12, 32 } }); |

