How generics are implicitly converted __ generic type

Source: Internet
Author: User
Tags what parameter

Between objects, we create objects through polymorphism, the parent class compiles, and the subclass runs, which achieves the reusability and decoupling of the code. However, we found that in generics the type of list<> restriction, we cannot compile and run by polymorphism:list<basea> List = new list<suba> (), and found that the following error occurred


1. Why use this method

2. How to Solve

3. Why this phenomenon occurs

In my current situation, the overload of the method is avoided, and the method implementation is the same, and if this is not possible, then we will have to use the overload of the method and write too much of the same code simply because the parameter types are passed differently.

As in the following code form:

Class Program
{
static void Main (string[] args)
{

Defining the Parent class object
Basea Basea;
SubA SubA = new SubA ();

The parent class Gets the method return value, and the argument is a subclass object
Basea = Testobject (SubA);
Subb Subb = new Subb ();

Avoid overloading of methods
Basea = Testobject (Subb);

In generics, we want to avoid overloads of methods like Polymorphic implementations, but in fact it appears to be the following error

list<suba> subalist = new list<suba> ();
Testlist (subalist);//Error point


Console.readkey ();
}
private static list<basea> testlist (list<basea> List)
{
list<basea> listbase = new list<basea> ();
for (int i = 0; i < list. Count; i++)
{
Listbase.add (List[i]);
}
return listbase;
}
private static Basea Testobject (Basea Basea)
{
return Basea;
}
}
Class Basea {}
Class suba:basea{}
Class Subb:basea {}

Why you use this method.

So I hope that the scope of the open parameter interface, such as polymorphism, achieves the reusability of the code.

The argument we want to receive in the method can be a generic parameter, and when you call the method, you can run my method regardless of what parameter you pass in. When we pass the object, we are defining the parameter type as the parent type, passing the subtype to it when the method is called, and the method will still function correctly. Therefore, we hope that in the transfer of generics, we can also use the upward transition mechanism to achieve our desired purpose.

In solving the problem, we can also use the Parameter object as an alternative, but there is no doubt that we will face the performance loss between boxing and unboxing. And if you use generics, when the object calls the method, tell it to the known object and let its object change accordingly, the problem we want to avoid can be solved.

How to solve the problem.

In Java, you can find that you can use wildcard characters or declare them as type T to perform operations.


It can be found that when invoking a method testlist, we can pass any object in and do not need to have an inheritance relationship, because when declaring a generic, it can only be declared as equivalent and cannot be implicitly converted.

We hope that C # can also be declared similarly. We found that C # can achieve this effect in a similar way.

Generic class:

public class Listclass<t>
{
public static void GetList (List<t> List)
{


}
}

Generics on a method

private static void Getlist<t> (List<t> List)
{


}

As a result, we have achieved consistent results.

why this phenomenon occurs.

What puzzled me most in the process was how the type set in the generic container could not have polymorphic relationships like a single object. In the process of looking at Java, also one of the doubts, in the use of its containers can only be judged, and the internal properties of the container is unknown.

Above is purely personal idea idea, generic type still have a lot of place that do not understand, hope can help revise out, thank.

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.