You can have both generic and non-generic methods in a generic class. The non-generic methods can still make use of the class’ generic type parameters.
In the example below, the BuryThing method accepts a parameter whose type matches the type parameter of the class itself.
1
2
3
4
5
6
7
| public class Dog<T> { // Not classified as generic method because it does not introduce new type parameter public void BuryThing(T thing) { Console.WriteLine( string .Format( "{0} is burying: {1}" , Name, thing.ToString())); } |
When we invoke this method, we must pass it an object whose type matches the type parameter that the class was constructed with.
1
2
| Dog<Cow> d = new Dog<Cow>( "Buster" , 5); d.BuryThing( new Cow( "Bessie" )); |