I have two classes and an interface like
interface IVehicle
{
void Drive();
}
class Benz :IVehicle
{
public void Drive()
{
Console.WriteLine("WOW! driving benz");
}
}
class Ferrari : IVehicle
{
public void Drive()
{
Console.WriteLine("WOW! driving ferrari");
}
}
I got a Driver class which uses this.
class Driver
{
public void StartDriving(IVehicle vehicle)
{
vehicle.Drive();
}
}
There is one more driver which is generic.
class GenericDriver
{
public void StartDriving<T>() where T : IVehicle , new()
{
T vehicle = new T();
vehicle.Drive();
}
}
Questions
- Do you see any advantages for the generic implementation compared to normal Driver? If yes, what are they?
- Which one do you prefer? A generic one or the normal one?
- Is there a better way to implement a generic driver?
- I am getting a feeling that generics in C# is very limited when compared with C++ templates. Is that true?
Any thoughts?