views:

221

answers:

3

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

  1. Do you see any advantages for the generic implementation compared to normal Driver? If yes, what are they?
  2. Which one do you prefer? A generic one or the normal one?
  3. Is there a better way to implement a generic driver?
  4. I am getting a feeling that generics in C# is very limited when compared with C++ templates. Is that true?

Any thoughts?

+9  A: 
  1. Absolutely no advantages in this case whatsoever. Except if you really want to create an instance of T in Drive(), which can be done without generics with delegate IVehicle VehicleBuilder();
  2. It depends on the situation. But generally speaking I'd prefer first.
  3. Again: it depends on what you want to do.
  4. Yes, this is true. Remember though, that C++ templates are compile-time (JIT time) constructs, whereas C# generics are run-time constructs.

Now on why would I want a generic Driver. Consider:

class Driver<TVehicle>
    where TVehicle : class, IVehicle, new()
{
    public TVehicle Vehicle { get; set }

    public Driver()
    {
        Vehicle = new TVehicle();
    }
}

This way I'll be able to use a strongly-typed Driver<>.Vehicle property which will be of a particular type, rather than of a more common IVehicle.

Anton Gogolev
Thanks. That makes sense.
Appu
Generics are mainly a tool to obtain type safety. The binary code will be shared by all uses of the generic type but the compiler will ensure the types are correct. Compare with C++, where implementations (binaries) will be specific for each and every type, allowing for even different implementations of the template depending on arguments.
David Rodríguez - dribeas
+2  A: 
  1. I don't really see any advantages in your example, but they do different things. The normal one has the ability to use an already created vehicle, while the generic one can only create a new one.
  2. Since these methods do different things, it depends. Whichever works better for your code. Personally I would use the normal one.
  3. I'm not really sure why you even want to use generics. It doesn't help you much in this case.
  4. Yes, C++ templates are much more powerful. Just look at all the weird Boost libraries. This is mainly because C++ templates are done at compile time, not run time/JIT time like in .NET.
Zifre
+1  A: 

The second means you need to construct a new object every time it is called, whereas the first would allow you to construct outside of the call and hence reuse the object, which could save a lot on resources if called often.

ck