C# Generic Methods


This entry is part 4 of 7 in the series C# Generics

Unlike the other generics, a method is not a type but a member. You can declare generic methods in both generic and non-generic classes and in structs and interfaces. To say that again, generic methods can be declared in generic and non-generic types.

By the way, Mosh Hamedani has a video series at Udemy.com that covers Generics. The video is the first one in the Advanced C# series of videos.

Declaring a Generic Method

This is on page 462 of the book Illustrated C# 7: The C# Language Presented Clearly, Concisely, and Visually 5th Edition by Daniel Solis (Author), Cal Schrotenboer (Author).

We read where S: Person as “where the type S is a Person”. Person is a class that we created.

public void PrintData<S, T> ( S p, T t ) where S: Person
{
   // ... Method parameter list
}
Series Navigation<< C# Generic ConstraintsC# Enumerators >>