4

I've been reading on leveraging generic constraints, I've found that generics can be constrained to struct, class, new, Class and Interface. The reasons behind the first three are pretty obvious. But I can't really understand why and when to constraint to a class. i.e

class Foo<T> where T : Bar 
class Foo<T> where T : IBar

This allows us to constrain to Bar and it's children, and IBar and implementers respectively. Can't we just program to the class, or the interface ? Because that's basically what polymorphism is for, and Microsoft Engineers are far from dumb to implement a useless feature.

What am I missing ?


Update:

My question has been marked as duplicate for : Why use generic constraints in C#

I don't think it's the case.

I went through this question thoroughly, and didn't really find the answer: It discussed the use of generic constraints in general, while here I'm asking about why do we constrain to a class or an interface, why not just program to it directly ?

Community
  • 1
  • 1
Wahib Mkadmi
  • 627
  • 2
  • 9
  • 26
  • 2
    You mean something like [this](https://msdn.microsoft.com/en-us/library/bb396189(v=vs.110).aspx)? There is [another approach](https://msdn.microsoft.com/en-us/library/bb669096(v=vs.100).aspx) which supports any type. – Tim Schmelter Jul 07 '16 at 15:34
  • 2
    For me is useful constrain to a class when I need from generic T create a class with new keyword – Alexandr Jul 07 '16 at 15:35
  • 2
    This is NOT a duplicate, the question is asking why make `Foo` generic with a class constraint instead of just implementing `Foo` hard-coded against the class. The asker understands why constraints are useful when used for interfaces but not why they are useful against a class. – Lukazoid Jul 07 '16 at 15:44
  • I'm wondering why the rush to mark this question as duplicate. The difference between the two questions is pretty obvious though. – Wahib Mkadmi Jul 07 '16 at 15:45
  • Interface can be implemented by value type, and using generics can eliminate boxing. – user4003407 Jul 07 '16 at 15:50
  • 2
    @WahibMkadmi As I can't post a full answer I will give you a brief answer here. By using a generic you are able to get values out of `Foo` which are more specific than `Bar`. For example if you had a method which returned T, `Foo` could return `BarExtended` for this method instead of just `Bar` which may expose more functionality than that exposed by `Bar`. This way some places may use `Foo` while others may just use `Foo`. – Lukazoid Jul 07 '16 at 15:50
  • 2
    You may want to show hypothetical sample code for "just program to it". This will clarify what you are looking for – Alexei Levenkov Jul 07 '16 at 15:57
  • Thanks for your interest everyone. The question has already been closed by Citizen Patrols: such a friendly environment. I'll just seek help elsewhere. – Wahib Mkadmi Jul 07 '16 at 16:07
  • @WahibMkadmi I have used the class constraint when I had mutliple classed derived from an abstract class. Using the abstract class, you can constrain the generic to corresponding derived classes while also exposing public functionality and parameters of the abstract class. – Larry Jul 07 '16 at 16:41
  • Note that editing your question was the right thing to do to get your question reopened. Note that insulting the people who closed the question was not the right thing to do to get your question reopened. – Heretic Monkey Jul 07 '16 at 18:16

1 Answers1

4

Perhaps this simple example might help.

If I have these classes:

public class ListOfCars<T> : List<T> where T : Car { }

public abstract class Car { }
public class Porsche : Car { }
public class Bmw : Car { }

...and then if I write this code:

var porsches = new ListOfCars<Porsche>();

// OK
porsches.Add(new Porsche());

//Error - Can't add BMW's to Porsche List
porsches.Add(new Bmw());

You can see that I can't add a BMW to a Porsche list, but if I just programmed off of the base class it would be allowed.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172