C # generic interface

Source: Internet
Author: User

You can use generics to define interfaces. methods defined in interfaces can contain generic parameters.

For example, we need to implement an IComparable interface to compare the attributes of two classes of objects. Traditionally, we will do this:

Public class Person: IComparable {public string LastName {get; set;} public Person (string lastName) {this. lastName = lastName;} public int CompareTo (object obj) {Person other = obj as Person; // mandatory conversion is required here before comparing attributes return this. lastName. compareTo (other. lastName );}}
Let's take a look at the changes that will happen after the introduction of generic interfaces:

Similarly, we still need to implement the IComparable interface. The difference is that this is a generic interface.

Public class Person: IComparable <Person> {public string LastName {get; set;} public Person (string lastName) {this. lastName = lastName;} public int CompareTo (Person other) {return this. lastName. compareTo (other. lastName); // you can operate the attribute directly here, without the previous conversion process }}

Then you can test the above Code in the main function:

Static void Main (string [] args) {Person [] person = new Person [] {new Person ("Microsoft"), new Person ("google ")}; int result = person [0]. lastName. compareTo (person [1]. lastName); Console. writeLine (result); // output 1}

The above example illustrates that a generic interface is an interface with a generic type, which has more type constraints than a common interface .. NET1.0 has an IComparable Interface Based on objects. IComparable <in T> is based on a generic type:

    public interface IComparable<in T>    {        int CompareTo(T other);    }

Next, we will talk about two concepts related to generic interfaces: covariant and resistance to variant.

First define a base class Shape:

    public class Shape    {        public double Width { get; set; }        public double Height { get; set; }        public override string ToString()        {            return string.Format("width:{0},height:{1}", Width, Height);        }    }
Define another generic interface: the generic type is marked with the out keyword, indicating that the generic interface is covariant (that is, the return type can only be T ), and return this type from a read-only index.

    public interface IDisPlay<out T>    {        T this[int index] { get; }    }

Next, define a Rectangle subclass to inherit the Shape class and implement the generic interface IDisPlay (out T ).

    public class Rectangle : Shape, IDisPlay<Shape>    {        Shape IDisPlay<Shape>.this[int index]        {            get            {                if (index != 0)                {                    throw new ArgumentOutOfRangeException("index");                }                else                {                    this.Width = 400;                    this.Height = 500;                    return this;                }            }        }    }

Finally, let's test the result:

Static void Main (string [] args) {IDisPlay <Shape> shapeDisplay = new Rectangle (); // assign the class object that implements the generic interface to the Console of the generic interface. writeLine (shapeDisplay [0]. toString (); // This object is returned for access through the index tool of the generic interface}
The expected result is indeed output: width: 400, height: 500.

If the generic type is marked with the in keyword, the generic interface is variable-resistant.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.