简体   繁体   English

C#如何创建将数组作为参数传递的方法?

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

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

Intellisense不会将array1显示为可用,并且VS2010的变量用红色下划线

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

与C ++不同,在C#中,必须将方括号放在Type的末尾:

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

As Rob showed, you've got your square brackets in the wrong place. 如Rob所示,您将方括号放在错误的位置。

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. IMO用这种方式更有意义-将所有类型信息放在一个地方。 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). 有关数组的一般信息,请参见C#4规范的第12章,其中包括“数组类型”(12.1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM