As already pointed out, there's no standard way to convert a
string
to a specified class. But what you
can do is pass in a delegate to do the conversion, which pushes that responsibility back onto the caller:
class MyGenericArray<T>
{
private readonly T[] arr;
public MyGenericArray(int size, Func<string, T> parseValue)
{
arr = new T[size];
Console.WriteLine("Pls enter {0} no of values", size);
for (int i = 0; i < size; i++)
{
string value = Console.WriteLine();
arr[i] = parseValue(value);
}
}
Usage:
MyGenericArray<int> numbers = new MyGenericArray<int>(42, int.Parse);
MyGenericArray<Person> people = new MyGenericArray<Person>(5, value => new Person(value));
Using Delegates (C# Programming Guide) | Microsoft Docs[
^]
Lambda Expressions (C# Programming Guide) | Microsoft Docs[
^]