I have a variable usersInput which has a value Console.ReadLine()
I want to add this variable to the array built with List
static void Resize<T>(ref List<T> array, string name)
{
array.Add(name);
Console.WriteLine();
for (int i = 0; i < array.Count; i++)
{
Console.WriteLine(array[i]);
}
}
static void Main(string[] args)
{
List<string> teachers = new List<string> { "1", "2", "3"};
Console.WriteLine("Enter a name");
string userInput = Console.ReadLine();
Resize(ref teachers, userInput);
}
>Solution :
Why do you need Resize<T> method at all? You could do it easier:
static void Main(string[] args)
{
List<string> teachers = new List<string> { "1", "2", "3" };
Console.WriteLine("Enter a name");
string userInput = Console.ReadLine();
if (string.IsNullOrWhiteSpace(userInput) || !string.IsNullOrEmpty(userInput))
{
teachers.Add(userInput);
}
// if want to change list to array then use
// var arr = teachers.ToArray();
foreach (var teacher in teachers)
{
Console.WriteLine(teacher);
}
}