Restrictions on variability in C,

Source: Internet
Author: User

Restrictions on variability in C,

It is found that there are few Chinese Blog posts that focus on the limitation of C # variability (or a lot of messy content in a large text segment), so I wrote a blog post to focus on the discussion, these restrictions are based on security considerations or are hard to implement.

Note: This article does not explain what variability is, and the issues discussed in this article are based on. NET 4 to. NET 4.5. I strive to be concise everywhere.

Okay. Let's get started.

 

1. variability only supports reference conversion, prohibiting value type conversion, packing conversion (well, actually, value type conversion), and user-defined type conversion.

Any variability conversion is a type of reference conversion. This type of conversion is type-safe. It can only operate on the reference type, without affecting the binary representation of the reference or creating new objects.

 

2. Only interfaces and delegation can have variable type parameters.

 

3. The parameter modified by the out parameter remains unchanged.

This is not intuitive.

For example, a delegate int GetXXX <T> (string name, out T value)

At this time, T cannot be changed.

Because the CLR does not have the out parameter, (the ref and out functions cannot be reloaded), The out parameter is a ref parameter containing the [Out] feature, the ref parameter means that (for CLR) data can be input or output, so T is unchangeable.

 

4. The specified variability (in/out) must be displayed instead of being inferred by the compiler.

Forcing the developer to display a specified display can enhance security. For example, when a new change is available, the overload may be affected.

 

5. Do not use variability in multicast delegation.

This should be a bug ...... Such code should not be allowed, and problems cannot be found during compilation. One strategy is to create a target-type delegate for the delegate that will generate a covariant or inverter to enclose it.-Of course, if you are aware of this problem, you are unlikely to write such code again.

 

Write it here for the moment.

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.