简体   繁体   中英

C# arrays in methods

I was trying to do something like:

static void Main(string[] args)
{
  string[] clients = new clients[0];
  createClients(clients);
  //do something with clients
}

static void CreateClients(string[] clients)
{
  //ask how many clients
  int c = Convert.ToInt32(Console.ReadLine());
  clients = new string[c];
}

But when I go out of the CreateClients procedure the 'clients' array was not modified, am I missing something? I thought arrays were passed always as reference

Thanks

You need to pass the clients array by reference.

createClients(ref clients);

static void CreateClients(ref string[] clients)
{
  //ask how many clients
  int c = Convert.ToInt32(Console.ReadLine());
  clients = new string[c];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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