Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# inheritance in <T>

Tags:

c#

inheritance

ow to indicate that T is inherited from other class that implements certain methods:

public Class A
 {
    public string GetAccessPoint();
    public string GetPriorityMap();
 }

public Class IndexBuilder<T> where T : A
{
   List<string> Go<T>(T obj)
   {
      string aPt=obj.GetAccessPoint();
      string pMap=obj.GetPriorityMap();
   }
}

In other words, I cannot access GetAccessPoint and GetPriority map of the obj although I indicated that it is inherited from A.

like image 488
AstroSharp Avatar asked Dec 01 '22 21:12

AstroSharp


1 Answers

That's because you redefined what T is when you made the Go method generic. Since T is defined at the class level, there is no need to redefine it in Go. Try this:

public Class IndexBuilder<T> where T : A
{
   List<string> Go(T obj)
   {
      string aPt=obj.GetAccessPoint();
      string pMap=obj.GetPriorityMap();
   }
}
like image 85
Chris Shain Avatar answered Dec 31 '22 08:12

Chris Shain