C# Generics

Ronald Rex 1,666 Reputation points
2023-09-22T15:26:54.0333333+00:00

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()
      {
            

      }
  }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,819 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,656 Reputation points
    2023-09-22T16:06:51.8166667+00:00

    This will create an array of zero elements:

    Employee[] e = new Employee[0];
    

    But you're assigning an employee to the first element beneath it. If you change that 0 to a 1 it shouldn't throw an exception when you try to index into it.

    Also you need to add an Employee instance into that zero'th element before you can change it's properties:

    e[0] = new Employee();
    

    or you could make Employee a struct instead of a class and then the employees in the array would be allocated as part of the array's allocation.


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 63,741 Reputation points
    2023-09-22T16:36:16.3633333+00:00

    you also need to create an employee for the array

            Employee[] e = new Employee[1];
            e[0] = new Employee();
            e[0].Id = 1;
            e[0].Name = "Ronald";
            e[0].Age = 30;
    

    or cleaner

            Employee[] e = 
            {
                new Employee
                {
                    Id = 1,
                    Name = "Ronald",
                    Age = 30
                }
            };
            ```
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.