Java, generic type wildcard and C # control

Source: Internet
Author: User

C # generics do not have a type wildcard because. NET generics are generics supported by the CLR, and Java's JVM does not support generics, just syntax sugar, which is converted to type object at compiler compile time

Type Wildcard in Java represents the parent class of a generic type

public void Test (list<object> c)  {for        (int i = 0;i < C.size (); i++)        {               System.out.println (c.get (i));        }  }  

Create a List<string> object  list<string> strlist = new arraylist<string> ();  The previous test method  test (strlist) is invoked with Strlist as a parameter;  

Compile the above program, test (Strlist) will have a compile error, meaning that list<string> can not be considered as a subclass of list<object>. This is the time to use the type wildcard, the wildcard is a number

The above list<object> replaced with list<?> to be able to compile the


public void Test (list<?> c)  {for        (int i = 0;i < C.size (); i++)        {               System.out.println (C.get (i));        }  }  

List<string> can be used as a subclass of List<?>, list<?> can be used as a parent of whatever List type,

Suppose you just want to be the parent of list<string>, not list<int>? Written such list<?

Extends string>

Constraining the generic type in C # is like this


Class Myclass<t, u>      where T:class      where u:struct  {}  

Interface IMyInterface  {  }    class Dictionary<tkey, tval>      where tkey:icomparable, IEnumerable      where Tval:imyinterface  {Public      void Add (TKey key, Tval val)      {      }  }  

upper bound generic wildcard characters in Java:

Indicates that the T type must be a number class or its subclass, and must implement the Java.io.Serializable interface public  class Apple<t extends number & Java.io.serializable>  {}  


Java, generic type wildcard and C # control

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.