Csharp/CSharp Tutorial/Generic/Generic Interface

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Declaring a Generic Interface, Implementing a Generic Interface

interface IPair<T>
{
    T First
    {
        get;
        set;
    }
    T Second
    {
        get;
        set;
    }
}
public struct Pair<T>: IPair<T>
{
    public T First
    {
        get
        {
            return _First;
        }
        set
        {
            _First = value;
        }
    }
    private T _First;
    public T Second
    {
        get
        {
            return _Second;
        }
        set
        {
            _Second = value;
        }
    }
    private T _Second;
}

Declaring a Generic with Multiple Type Parameters

interface IPair<TFirst, TSecond>
{
    TFirst First
    { get; set;    }
    TSecond Second
    { get; set;    }
}
public struct Pair<TFirst, TSecond>: IPair<TFirst, TSecond>
{
    public Pair(TFirst first, TSecond second)
    {
        _First = first;
        _Second = second;
    }
    public TFirst First
    {
        get{ return _First;    }
        set{ _First = value; }
    }
    private TFirst _First;
    public TSecond Second
    {
        get{ return _Second; }
        set{ _Second = value; }
    }
    private TSecond _Second;
}

Generic IEquatable

using System;
using System.Collections.Generic;
using System.ruponentModel;
    public sealed class Pair<TFirst, TSecond>: IEquatable<Pair<TFirst, TSecond>>{
        private readonly TFirst first;
        private readonly TSecond second;
        public Pair(TFirst first, TSecond second)
        {
            this.first = first;
            this.second = second;
        }
        public TFirst First
        {
            get { return first; }
        }
        public TSecond Second
        {
            get { return second; }
        }
        public bool Equals(Pair<TFirst, TSecond> other)
        {
            if (other == null)
            {
                return false;
            }
            return EqualityComparer<TFirst>.Default.Equals(this.First, other.First) &&
                   EqualityComparer<TSecond>.Default.Equals(this.Second, other.Second);
        }
        public override bool Equals(object o)
        {
            return Equals(o as Pair<TFirst, TSecond>);
        }
        public override int GetHashCode()
        {
            return EqualityComparer<TFirst>.Default.GetHashCode(first) * 37 +
                   EqualityComparer<TSecond>.Default.GetHashCode(second);
        }
    }

Generic Interface

using System;
using System.Collections.Generic;
interface GenericInterface<T>              
{
   T getValue(T tValue);
}
class MyClass<T> : GenericInterface<T>     
{
   public T getValue(T tValue)    
   {
      return tValue;
   }
}
class MainClass
{
   static void Main()
   {
      MyClass<int>    intObject    = new MyClass<int>();
      MyClass<string> stringObject = new MyClass<string>();
      Console.WriteLine("{0}", intObject.getValue(5));
      Console.WriteLine("{0}", stringObject.getValue("Hi there."));
   }
}
5
Hi there.

Generic Interface for binary operation

using System;
using System.Collections.Generic;
using System.Text;
  public interface IBinaryOperations<T>
  {
    T Add(T arg1, T arg2);
    T Subtract(T arg1, T arg2);
    T Multiply(T arg1, T arg2);
    T Divide(T arg1, T arg2);
  }
  public class BasicMath : IBinaryOperations<int>
  {
    public BasicMath() {}
    public int Add(int arg1, int arg2)
    { return arg1 + arg2; }
    public int Subtract(int arg1, int arg2)
    { return arg1 - arg2; }
    public int Multiply(int arg1, int arg2)
    { return arg1 * arg2; }
    public int Divide(int arg1, int arg2)
    { return arg1 / arg2; }
    }
  class Program
  {
    static void Main(string[] args)
    {
      BasicMath m = new BasicMath();
      Console.WriteLine("1 + 1 = {0}", m.Add(1, 1));
    }
  }

Implement multiple generic interfaces by a non-generic class

using System;
using System.Collections.Generic;
interface GenericInterface<T>                            
{
   T getValue(T inValue);
}
class MyClass : GenericInterface<int>, GenericInterface<string>    
{
   public int getValue(int inValue)            
   {
      return inValue;
   }
   public string getValue(string inValue)      
   {
      return inValue;
   }
}
class MainClass
{
   static void Main()
   {
      MyClass TrivInt = new MyClass();
      MyClass TrivString = new MyClass();
      Console.WriteLine("{0}", TrivInt.getValue(5));
      Console.WriteLine("{0}", TrivString.getValue("Hi there."));
   }
}
5
Hi there.