.NET Course
  • Welcome developers
  • LINQ
    • Advanced c# overview
      • Implicit typing
      • Generics
      • Attributes
      • Reflection
      • Delegates
      • Anonymous Methods and Lambda Expressions
      • Events
      • Anonymous Types
      • Ref vs Out
      • Task
      • TaskFactory
      • Asynchronous programming with async and await
      • Abstract class VS Interface
      • Inheritance, Composition and Aggregation in C#
    • LINQ Tutorial
      • Loading Related Entities
      • Lambda Expression
      • Standard Query Operators
    • Testing ASP.NET Core
      • Implementing unit tests for ASP.NET Core Web APIs
    • Software development principles
      • Object-Oriented Programming (Principles)
      • N-tier architecture style
      • Command and Query Responsibility Segregation (CQRS) pattern
      • SOLID: The First 5 Principles of Object Oriented Design
  • Helping links
    • SonarQube installation and tutorial for .net core projects
  • C# .NET Interview Questions
    • Ultimate C# Interview questions
      • Part 4
      • Part 3
      • Part 2
      • Part 1
  • Unit test .NET Core xUnit & MOQ
    • Content
      • Snippets
      • Traits
Powered by GitBook
On this page

Was this helpful?

  1. LINQ
  2. Advanced c# overview

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();
}
PreviousImplicit typingNextAttributes

Last updated 3 years ago

Was this helpful?