88 votes
Accepted

What is generics abuse?

When generics are appropriately applied, they remove code instead of just rearranging it. Primarily, the code that generics are best at removing is typecasts, reflection, and dynamic typing. ...
Karl Bielefeldt's user avatar
64 votes

Is it a code smell to store generic objects in a container and then get object and downcast the objects from container?

Yes, it is a code smell (in lots of cases). I think it is difficult to replace if-else with virtual methods in tools In your example, it is quite simple to replace the if/else by virtual methods: ...
Doc Brown's user avatar
  • 206k
54 votes
Accepted

If my IDE is so smart, why do I need to cast "clone()"?

why do I have to cast the returned object of Object.clone()? Because it returns Object. The generics framework makes me think the IDE could check the type of object references on the right-side of ...
Jörg W Mittag's user avatar
49 votes
Accepted

C# Generics - How to avoid redundant method?

Add a proxy interface (sometimes called an adapter, occasionally with subtle differences), implement LogicToBeApplied in terms of the proxy, then add a way to construct an instance of this proxy from ...
Jack's user avatar
  • 4,469
26 votes

If my IDE is so smart, why do I need to cast "clone()"?

This is not a feature of any IDE, but of the language definition. An IDE helps you use a programming language more efficiently, it doesn't change the semantics of that language. That means that an ...
Kilian Foth's user avatar
26 votes
Accepted

Should one prefer a generic version of a function, even if it's not re-used (yet)?

There are definitely cases where solving a more general problem than required makes code easier to read, to reason about and to maintain. The most simple example I can think of is when code deals with ...
Doc Brown's user avatar
  • 206k
23 votes

Is it a code smell to store generic objects in a container and then get object and downcast the objects from container?

The major problem with your code is, that whenever you introduce any new item, you not only have to write and update the item's code, you also have to modify your player (or wherever the item is used),...
Mario's user avatar
  • 1,499
16 votes

Why Generic type can not instantiated?

This is impossible because of the following 2 reasons. There is no guarantee that T has a no-args constructor (and for that matter isn't an interface or abstract class) Due to type erasure (required ...
Richard Tingle's user avatar
16 votes

Generics vs common interface?

"most likely clients of this Repository will want to get and use objects through IBusinessObject interface". No, they won't. Let's consider that the IBusinessObject has the following ...
Lucas Corsaletti's user avatar
14 votes

Should one prefer a generic version of a function, even if it's not re-used (yet)?

There's has been some back and forth in the comments, and my feedback generally boils down to the same argument every time: Your problem, as you describe it, always starts from the really unusual ...
Flater's user avatar
  • 48.7k
13 votes

Why use a generic method with a type constraint instead of the type itself?

Is this a case of an overzealous developer learning about generics before learning about ordinary inheritance? Yes, it probably is. Or is there a legitimate reason why a method signature would be ...
Telastyn's user avatar
  • 109k
12 votes

C# Generics - How to avoid redundant method?

Here is an outline how to use adapters without inheriting from A and/or B, with the possibility of using them for existing A and B objects: interface IAdapter { int Property { get; set; } } ...
Doc Brown's user avatar
  • 206k
12 votes
Accepted

Is there a name for this construct with generics?

Is there a name for this? Ok, I may be misunderstanding your scenario, but I think what you're describing is generally called the "Curiously Recurring Template Pattern", CRTP for short. It ...
Erik Eidt's user avatar
  • 33.7k
12 votes

Are interfaces made useless by the template pattern?

I think there is a subtle distinction you're missing between guaranteeing consistency and guaranteeing truth. When you write public class Sandwich : IPizza you are telling the compiler that a Sandwich ...
IMSoP's user avatar
  • 5,817
11 votes
Accepted

Why generic interface cannot implement dynamic type?

This is not allowed, as Chris Burrows (who helped create and implement dynamic) explains: Well, for one thing, it doesn’t actually give you anything that you didn’t already have. The first thing ...
Mason Wheeler's user avatar
11 votes
Accepted

Is inheritance constraint on Generics redundant? If not what is its recommended use?

If it would be only about classes, and only about one class, your argument could be valid: you could perfectly perform manually the type substitution in the code. But you would hardwire the choice of ...
Christophe's user avatar
  • 76.6k
9 votes
Accepted

Why covariance is forbidden for abstract class in C#

Covariance and contravariance are not allowed in any class. As abstract classes can have concrete implementation, this applies to them too. The short answers for why is that (a) the CLR doesn't ...
David Arno's user avatar
  • 39.2k
9 votes

C# Generics - How to avoid redundant method?

You could adapt ClassA and ClassB through a common interface. This way your code in LogicAToBeApplied stays the same. Not much different than what you have though. class A { public int Property { ...
devnull's user avatar
  • 2,979
9 votes
Accepted

What is the relationship between variance, generic interfaces, and input/output?

"In a general sense, covariance is achieved when the direction of assignment compatibility is preserved. Contravariance is achieved when the direction of assignment compatibility is reversed.&...
Filip Milovanović's user avatar
8 votes

Is it a code smell to store generic objects in a container and then get object and downcast the objects from container?

While @DocBrown has given a good answer, it doesn't go far enough, imho. Before you start evaluating the answers, you should evaluate your needs. What do you really need? Below I will show two ...
Polygnome's user avatar
  • 2,046
8 votes

C# Generics - How to avoid redundant method?

The C++ version only works because its templates use “static duck typing” – anything compiles as long as the type provides the correct names. It is more like a macro system. The generics system of C# ...
amon's user avatar
  • 134k
8 votes

C# Generics - How to avoid redundant method?

If you really want to throw caution to the wind, you can use "dynamic" to make the compiler take care of all the reflection nastiness for you. This will result in a runtime error if you pass an ...
OldFart's user avatar
  • 199
8 votes
Accepted

Is there any advantage using generic type which implements interface over direct interface usage?

It is an advantage if you want to create a command processor for a specialized command type. E.g. you have an interface public interface IUndoableCommand : ICommand { void Undo(); } Now you can ...
Olivier Jacot-Descombes's user avatar
8 votes

What is the relationship between variance, generic interfaces, and input/output?

Let's take a step back: what does variance actually mean? Well, variance means "change" (think of words like "to vary" or "variable"). co- means "together" (...
Jörg W Mittag's user avatar
7 votes
Accepted

Is there a programming language with objects and generics, but without inheritance?

Rust has objects and general-purpose generics but not inheritance.
JacquesB's user avatar
  • 58.7k
6 votes
Accepted

When using generics for both in and out types, why is out type generally last?

That's the way function types are normally written: a → b is a function that takes an a and returns a b. (a, b) → c is a function that takes a tuple of (a, b) and returns a c. a → b → c is a ...
Jörg W Mittag's user avatar
6 votes
Accepted

C# is it acceptable to create a generic class and use an enum as generic type?

On a technical level, there's nothing wrong with using an enum as the type used in a generic type. However, I've noticed that in most cases where this is being suggested, there's a conflation between ...
Flater's user avatar
  • 48.7k
6 votes
Accepted

Changing the generic type when chaining operations

Changing the type of an existing object is simply not something that can be done in a typesafe manner. The object might be referenced in one place as a MyClass<A> and in another place as a ...
amon's user avatar
  • 134k
5 votes
Accepted

What's the benefit of generics in an in interface e.g. IEnumerable<HttpPostedFileBase>

Because then IEnumerable<T> can be used for any type T, not just HttpPostedFileBase. Most of the collections in the System.Collections.Generic namespace implement IEnumerable<T>. ...
Robert Harvey's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible