#540 – Non-Generic Methods in a Generic Class
March 15, 2012 1 Comment
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.
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.
Dog<Cow> d = new Dog<Cow>("Buster", 5); d.BuryThing(new Cow("Bessie"));