But it's not as bad as it sounds and the only way (AFAIK) to be able to work with both polymorphic objects and have the convenience (and type checking) of a generic counterpart. Limiting yourself to just a generic object/interface unfortunately breaks a lot of design patterns. It's also safe afaik since the only thing the generic version does is some casting and it's generic parameter enforces that casting.
but if you have a better way I'm genuinely interested to hear it
> Limiting yourself to just a generic object/interface unfortunately breaks a lot of design patterns.
Which ones and why?
> It's also safe afaik since the only thing the generic version does is some casting and it's generic parameter enforces that casting.
If you don't have a known constraint that is enforced by the type system then it can never be 'safe'.
>If you don't have a known constraint that is enforced by the type system then it can never be 'safe'.
I don't quite know what you mean by this. If you cast to your "T" then casting is ensured. I guess there's some margin for error in that you must enter your casting code to be correct, but it's pretty hard to mess that part up, granted not as good as enforced genericy. Note that in many instances you only initialize generic classes, but you needed to be extract a non-generic interface from then to pass to other classes.
I'd like to see an example of what you mean. You can absolutely pass generic values to other classes. If the type you're passing the generic value to is not generic then the method should be. If you are casting then 99 times out of 100 you have a problem with your code because the types aren't compatible. i.e.
static void DoSomethingGeneric<A>(A value) => ...
static void DoSomethingSpecific(string value) => ...
IEnumerable<int> array = new [] { 1, 2, 3, 4, 5 };
DoSomethingGeneric(array.First()); // Works
DoSomethingSpecific(array.First()); // Can only ever work if array is a string[]
> If you cast to your "T" then casting is ensured.If you 'get out' of the generic situation by casting to an object or dynamic, then at some point you're going to have to cast back to a concrete type. That is the point where your code will blow up. You're carrying ticking time bombs as values.
is the horrible bit. You should almost NEVER override with "new".
interface IValidator<T> {
bool Validate(T thingToValidate);
}
class ThingValidator : IValidator<Thing> {}
class ThingValidator : IValidator<OtherThing> {}
var validators = GetAllValidators();
What type is `validators`?So it's whatever type of T you pass to GetAllValidators.
interface IValidator
{
bool Validate(object item);
}
interface IValidator<T> : IValidator
{
bool Validate(T item);
}
abstract class Validator<T>
: IValidator<T> where T : class
{
public abstract bool Validate(T item);
bool IValidator.Validate(object item)
{
var typedItem = item as T;
if (typedItem == null)
throw new InvalidOperationException();
return Validate(typedItem);
}
}But it could be `IValidator<object>`, in the worst case. That being said, why not make a `class ThingValidator<T> : IValidator<T> {}`, for all the lack of detail we have?
There aren't enough details here to demonstrate the weaknesses of generics.