Generics
Generic Interfaces
A generic interface can describe behavior for a specific value type.
Generic Interfaces
GenericInterfaces.cs
using System;
interface IFormatter<T>
{
string Format(T value);
}
class IntFormatter : IFormatter<int>
{
public string Format(int value)
{
return "value=" + value;
}
}
class Program
{
static void Main()
{
int value = ;
IFormatter<int> formatter = new IntFormatter();
string formatted = formatter.Format(value);
Console.WriteLine($"value={value}");
Console.WriteLine($"formatted={formatted}");
}
}
using System;
interface IFormatter<T>
{
string Format(T value);
}
class IntFormatter : IFormatter<int>
{
public string Format(int value)
{
return "value=" + value;
}
}
class Program
{
static void Main()
{
int value = ;
IFormatter<int> formatter = new IntFormatter();
string formatted = formatter.Format(value);
Console.WriteLine($"value={value}");
Console.WriteLine($"formatted={formatted}");
}
}
using System;
interface IFormatter<T>
{
string Format(T value);
}
class IntFormatter : IFormatter<int>
{
public string Format(int value)
{
return "value=" + value;
}
}
class Program
{
static void Main()
{
int value = ;
IFormatter<int> formatter = new IntFormatter();
string formatted = formatter.Format(value);
Console.WriteLine($"value={value}");
Console.WriteLine($"formatted={formatted}");
}
}
generic interface
A generic interface uses a type parameter in its contract.