Generics

Generics introduce the concept of type parameters to .NET, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. For example, by using a generic type parameter T, you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operations, as shown here

// Declare the generic class.
public class GenericList<T>
{
    public void Add(T input) { }
}
class TestGenericList
{
    private class ExampleClass { }
    static void Main()
    {
        // Declare a list of type int.
        GenericList<int> list1 = new GenericList<int>();
        list1.Add(1);

        // Declare a list of type string.
        GenericList<string> list2 = new GenericList<string>();
        list2.Add("");

        // Declare a list of type ExampleClass.
        GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
        list3.Add(new ExampleClass());
    }
}

Another good example of Generics:

static void Main(string[] args)
{
    var result = new Report<bool, int>
    {
        MyProperty = 1,
        MyProperty2 = true,
        MyProperty3 = 4
    };

    var helper = new PrintInformation();
    helper.Print(result);
}

public class Report<T, U>
{
    public int MyProperty { get; set; }
    public T MyProperty2 { get; set; }
    public U MyProperty3 { get; set; }
}

And a real-life approach for Generics:

// Interface
Task<T> Add<T>(int userId, T data) where T: class;
// Interface
Task<T> Add<T>(int userId, T data) where T: class;

// Repository
public async Task<T> Add<T>(int userId, T data) where T: class
{
    // Add entity type
    await DataContext.AddAsync(data);

    // Save entity type
    var result = await DataContext.SaveChangesAsync();
    
    // Return result
    if (result > 0)
        return data;

    // Return default result
    return null;
}
// Controller
[Authorize]
[HttpPost("addResume/{userId}")]
public async Task<IActionResult> AddResume(int userId, EmployeeResume employeeResume)
{
    // Validate the current auth user.
    if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
        return Unauthorized();

    // Add resume
    // employeeResume is the type entity we need to add
    // Such method can be used for any other type
    var addResumeResult = await _iResumeHandler.Add(userId, employeeResume);

    // Validate if the resume exists
    if (addResumeResult != null)
    {
        var getResumeFromRepo = await _iResumeHandler.GetResume(userId);
        var resumeToReturn = _mapper.Map<EmployeeResumeDto>(getResumeFromRepo);
        return Ok(resumeToReturn);
    }

    // Return default value
    return NotFound();
}

Last updated