Cocoa Dev
Cocoa Dev

Reputation: 9561

C# how do you create a method that passes an array as an argument?

private bool DisplayErrorMessages(String array1[], String array2[])

Intellisense doesn't show array1 as available and VS2010 has the variable underlined in red

Upvotes: 1

Views: 109

Answers (3)

Nicolas
Nicolas

Reputation: 6524

Unlike in C++, in C# you have to put the square brackets at the end of the Type :

private bool DisplayErrorMessages(String[] array1, String[] array2)

Upvotes: 7

Jon Skeet
Jon Skeet

Reputation: 1504122

As Rob showed, you've got your square brackets in the wrong place.

However, you should understand that this isn't just about method parameters - it's everywhere you declare an array type variable. For example, local variables:

// Valid
String[] x = null;

// Invalid
String x[] = null;

It makes more sense this way IMO - it puts all the type information in one place. Why would you want to specify it "around" the variable? :)

See chapter 12 of the C# 4 spec for more about arrays in general, including "array types" (12.1).

Upvotes: 2

Rob Levine
Rob Levine

Reputation: 41358

private bool DisplayErrorMessages(String[] array1, String[] array2)

Upvotes: 10

Related Questions