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.publicclassGenericList<T>{publicvoidAdd(T input) { }}classTestGenericList{privateclassExampleClass { }staticvoidMain() { // Declare a list of type int.GenericList<int> list1 =newGenericList<int>();list1.Add(1); // Declare a list of type string.GenericList<string> list2 =newGenericList<string>();list2.Add(""); // Declare a list of type ExampleClass.GenericList<ExampleClass> list3 =newGenericList<ExampleClass>();list3.Add(newExampleClass()); }}
// Controller[Authorize][HttpPost("addResume/{userId}")]publicasyncTask<IActionResult> AddResume(int userId,EmployeeResume employeeResume){ // Validate the current auth user.if (userId !=int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))returnUnauthorized(); // Add resume // employeeResume is the type entity we need to add // Such method can be used for any other typevar addResumeResult =await_iResumeHandler.Add(userId, employeeResume); // Validate if the resume existsif (addResumeResult !=null) {var getResumeFromRepo =await_iResumeHandler.GetResume(userId);var resumeToReturn =_mapper.Map<EmployeeResumeDto>(getResumeFromRepo);returnOk(resumeToReturn); } // Return default valuereturnNotFound();}