'Input array value in the same line using array size

I have a array and user can fixed the array length.But i want to input array value in the single line. when user add more cahrecter in the array length show out of boundary. suppose

var len = int.Parse(Console.ReadLine());
char[] values = new char[len];

suppose len is 2, when user input in the same line abc and user press enter program show out of boundary.

but i do not this approach

  • a
  • b
  • c

i want input same line using array size



Solution 1:[1]

Due to the nature of c# arrays, they are static and cannot be resized. that is why you are getting the error when trying to insert a value into a memory location not belonging to the values array you created

  • it sounds like what you are looking for is something like this, per Caius Jard's comment
char[] array = Console.ReadLine().ToCharArray();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Caius Jard