C # generic interface

Source: Internet
Author: User

You can define an interface using generics, and the methods defined in the interface can have generic parameters.

For example, we will implement a IComparable interface to compare the properties of objects of two classes. The traditional we would do this:

 Public classperson:icomparable { Public stringLastName {Get;Set; }  PublicPerson (stringLastName) { This. LastName =LastName;}  Public intCompareTo (Objectobj) { person other= obj asperson;//you need to do a cast here before you can compare the properties        return  This. Lastname.compareto (Other.      LastName); }  }  

Let's take a look at what happens when you introduce a generic interface:

Again, we're going to implement the IComparable interface, but the difference is that it's a generic interface

 Public classPerson:icomparable<person>  {       Public stringLastName {Get;Set; }  PublicPerson (stringLastName) { This. LastName =LastName;}  Public intCompareTo (person other) {return  This. Lastname.compareto (Other. LastName);//This allows you to manipulate properties directly, without the previous conversion process.    }  }  

You can then 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   //   output 1 } 

The above example illustrates the point that a generic interface is an interface with a generic type, which is more restrictive than a normal interface. NET1.0 has an object-based IComparable interface, Icomparable<in T> is based on a generic type:

 Public Interface icomparable< in t>  {      int  CompareTo (T);  }  

Next, we talk about two concepts related to generic interfaces: covariant and anti-change.

First define a base class shape:

 Public classShape { Public DoubleWidth {Get;Set; }  Public DoubleHeight {Get;Set; }  Public Override stringToString () {return string. Format ("Width:{0},height:{1}", Width, Height); }  }  

Define a generic interface again: The generic type is labeled with the Out keyword, indicating that the generic interface is covariant (that is, the return type can only be T) and that the type is returned from a read-only indexer.

 Public Interface idisplay< out t>  {      this [intget;}  }   

Next, define a rectangle subclass that inherits the shape class and implements the generic interface IDisplay (out T).

 Public classRectangle:shape, idisplay<shape>{Shape IDisPlay<shape>. This[intIndex] {          Get          {              if(Index! =0)              {                  Throw NewArgumentOutOfRangeException ("Index"); }              Else              {                   This. Width = -;  This. Height = -; return  This; }          }      }  }  

Finally, let's test the results:

Static void Main (string[] args)  {      IDisPlaynew Rectangle ();  the class object that implements the generic interface is assigned to the generic interface      Console.WriteLine (shapedisplay[0]. ToString ()); // returns this object  through the indexer access to the generic interface }  

It does output the result we want: width:400,height:500.

If a generic type is labeled with the IN keyword, the generic interface is immutable.

C # generic interface

Related Article

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.