C # 's generics do not have a type wildcard because. NET generics are generics supported by the CLR, and Java JVMs do not support generics, just syntactic sugars, which are converted to object type at compile time
The type wildcard represents the parent class of a generic type in Java
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> ();
Call the previous test method
test (strlist) by strlist as a parameter;
Compiling the above program, test (Strlist) will have a compile error, meaning that list<string> can not be treated as a list<object> subclass. This is where the type wildcard is used, and the wildcard character is a number.
The above list<object> replaced by list<?> can be compiled
public void Test (list<?> c)
{for
(int i = 0;i < C.size (); i++)
{
System.out.println ( i));
}
List<string> can be used as a subclass of List<?>, and list<?> can be used as a parent of any List type.
What if you just want to be a list<string> parent, not a list<int>? Write such list<? Extends string>
Constraining the generic type in C # is this
Class Myclass<t, u>
where T:class
where u:struct
{}
Interface IMyInterface
{
}
class Dictionary<tkey, tval>
where tkey:icomparable, ienumerable< C11/>where tval:imyinterface
{public
void Add (TKey key, Tval val)
{
}
}
constraint generic wildcard upper bound 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>
{}
The above is a small series for everyone to bring Java generic type wildcard and C # Comparative analysis of all the content, I hope that we support cloud Habitat Community ~