Limitations on variability in C #

Source: Internet
Author: User

There are few Chinese posts that focus on C # variability limits (or a lot of clutter in a large piece of text), so write a blog post and focus on these limitations, which are based on security considerations or are fundamentally difficult to implement.

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

All right, nonsense, let's go.

1. immutability only supports reference conversions, disables value type conversions, boxing conversions (well, it's actually value type conversions), and user-defined type conversions.

Any denaturing transformation is a reference transformation that is type-safe and can only manipulate reference types, does not have an impact on the binary representations of the references, and does not create new objects.

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

The parameters of the 3.out modification are constant.

This is not very intuitive to say.

such as a delegate delegate int getxxx<t> (string name,out T value)

T is not covariant at this time.

Because the CLR does not have an out parameter, (ref and out are not allowed to overload the function), the out parameter is a ref parameter with an [out] attribute, and the ref parameter means that (for the CLR) the data can be either input or output, so T is immutable.

4. You must display the specified variability (in/out) instead of being inferred by the compiler.

Forcing developers to show that the designation can enhance security, such as when a new change is available, may affect overloading.

5. Do not use variability in multicast delegates.

This should count as a bug ... Such code should not be allowed, and the problem cannot be found at compile time. One strategy is to wrap a delegate that creates a target type for a delegate that will produce a covariant or contravariant--of course, if you are aware of the problem, you are unlikely to write such a code again.

That's it for the time being.

Limitations on variability in C #

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.