使用字符串数组作为对象参数

时间:2015-05-06 01:49:36

标签: c# arrays class arguments

所以我有一个具有字符串数组参数的类。我想要做的是将多个字符串存储到此类的数组中。代码看起来像这样:

//Class part of it. Class is called "Event"
public class Event
{
   public string[] seats = new string [75];

   public Event(string[] seats)
   {
      this.seats = seats;
   }
}

// the main code that uses "Event" Class

string[] seatnumber = new string[75];
Event show = new Event (seatnumber[]); //And that is where the error comes in. 

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:3)

在将其放入Event构造函数时,从s​​eatnumber中删除括号。

For reference.

答案 1 :(得分:3)

调用数组时,变量名称不需要括号[]

Event show = new Event(seatnumber);

它与您之前在代码中调用的相同:

this.seats = seats;

seats也是一个数组,虽然您在调用它时没有添加[],所以没有错误。