eg630
eg630

Reputation:

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

Upvotes: 0

Views: 48

Answers (1)

Jorge Juárez
Jorge Juárez

Reputation: 26

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];
}

Upvotes: 1

Related Questions