Limitations of generics

Source: Internet
Author: User

For code like, duplicate, only parameter, return value type is different, of course, is the generic method, generic class.

The use of generic methods, generic classes, can be a good way to improve code reuse.

However, it is not always a good time to adopt a generic type.

As mentioned above, arguments and return values are different, and you can use generics. However, these generics are used only on parameters, return values, and may not be appropriate if they are used within the body of the function to use the properties and methods inside these generics. At this point, there is bound to be a cast statement. The question is, what is converted into? If these generics are programmed for the interface, they can, of course, be turned into interfaces; otherwise, generics are not suitable for use in these methods. This is my previous time encountered problems and experience.

Not an example.


=========================================

With regard to the limitations of generic methods, I think of one way of coping: Using reflection to access generic objects.

See

http://blog.csdn.net/leftfist/article/details/49784855


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.