Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Generic interfaces

Tags:

c#

Where do generic interfaces are quite useful ? ( I am a beginner,so simple example will definitely helpful).

like image 672
user196546 Avatar asked Jun 17 '26 09:06

user196546


2 Answers

Its useful when you need an interface, but you also need to abstract the data type. Simple example

public interface IMyShape<T>
{
   T X { get; }
   T Y { get; }
}

public class IntSquare : IMyShape<int>
{
   int X { get { return 100; } }
   int Y { get { return 100; } }
}

public class IntTriangle : IMyShape<int>
{
   int X { get { return 200; } }
   int Y { get { return 200; } }
}

public class FloatSquare : IMyShape<float>
{
   float X { get { return 100.05; } }
   float Y { get { return 100.05; } }
}
like image 89
Andrew Keith Avatar answered Jun 18 '26 22:06

Andrew Keith


You can look at IEnumerable<T> to begin with.

like image 25
Pavel Minaev Avatar answered Jun 18 '26 23:06

Pavel Minaev