Reputation: 850
What is relation between MyClass
object and <T>
in
public class MyClass<T> where T : myGeneric
{
}
To Add to my question after DBM's reply
How is the implemention above is better than
IGenericInterface
{
int Bar();
}
public abstract GenericClass : IGenericInterface
{
public virtual int Bar()
{
return 1;
}
}
and then using the abstraft class to implement
public class MyClass : GenericClass
{
IGenericInterface GenericObject ;
}
Upvotes: 0
Views: 207
Reputation: 6401
Does not require you to cast MyClass to IGenericInterface
At compile time, all occurrences of T will be replaced with the actual class type that was used in the call, rather than the class being casted at runtime to the IGenericInterface.
Upvotes: 0
Reputation: 59037
You may want to read up on C# generics. The code you posted is a class called MyClass
that can take anything that derives from the class myGeneric
. The T
is a placeholder for the type.
You could, for example, do
public class myGeneric
{
public int Bar()
{
return 1;
}
}
public class MyClass<T> where T : myGeneric
{
public void DoSomething(T foo)
{
int x = foo.Bar();
}
}
So in the example above, x
would be 1.
Upvotes: 2