I have this console application where I can create an array of type int, double, and string which I pass to a Generic method with a signature like this.... displayElements<T>(T[] array). But what if I want to call this method using my own type like an Employee type. I am having trouble creating my Employee array to pass to this Generic Method. Any help would be greatly appreciated.
static void Main(string[] args)
{
//generic = "not specific to a particular data type"
// add<T> to: classes, methods, fields, etc.
// allows for code reusability for different data types
int[] intarray = { 1, 2, 3 };
double[] doublearray = { 1.0, 2.0, 3.0 };
string[] stringarray = { "John", "Smith", "3" };
Employee[] e = new Employee[0];
e[0].Id = 1;
e[0].Name = "Ronald";
e[0].Age = 30;
displayElements(e);
displayElements(intarray);
displayElements(doublearray);
displayElements(stringarray);
Console.ReadKey();
}
public static void displayElements<T>(T[] array)
{
foreach( T i in array)
{
Console.Write(i + " ");
}
Console.WriteLine();
}
public class Employee
{
public int Id { get; set; }
public string? Name { get; set; }
public int? Age { get; set; }
public Employee()
{
}
}