Pushpendra
Pushpendra

Reputation: 850

What is relation between the MyClass object and <T> in the given class

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

Answers (3)

armen.shimoon
armen.shimoon

Reputation: 6401

  1. Does not require you to cast MyClass to IGenericInterface

  2. 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

Daniel Mann
Daniel Mann

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

doogle
doogle

Reputation: 3406

"MyClass" is the class name, "<T>" is the generic parameter list.

Upvotes: 0

Related Questions