C # 2.0 static class[2]

Source: Internet
Author: User
Tags abstract constructor static class
Look at what is static class, I have long ago to be unable to declare an abstract sealed class and distressed, I have raised this question in CSDN, but did not get resonance. The answer I got was this:

is abstract and sealed used together? The purpose of defining abstract is to abstract the base class, and seal represents an entity class that cannot be inherited, completely different, and how can it be used together?
Abstract indicates that must be inherited, sealed that can not be inherited, these two are contradictory, how can be used together.

By looking at the IL code, we find that the static class is actually an abstract sealed class, except that the compiler checks the member modifiers of the static class at compile time.

Let's guess what MS developers think: Their goal is to get the effect of abstract sealed class, but to be distressed by semantic contradictions, people want to get a graceful solution, and then add a static keyword for C # 2.0 to solve The problem.

One might ask: Why not a static class = abstract class + static member limit with sealed restrictions? The reason is that inheriting a class with only static members is completely indistinguishable from writing a class again. Why do you say that? Because static is necessarily not virtual, what is the use of inheriting it?

Static class also has a limit, which is only inherited from the System.Object, why there is this limitation, the reason is similar to the above.

When it comes to the static keyword, I think of the static constructor, and thank MS developers for providing such a good feature. But C # provides a static constructor but does not provide a static destructor, but in the applied Microsoft. NET Framework Programming Book Jeffery It provides us with a way to achieve the same effect through System.AppDomain.DomainUnload events.

Using static constructor It is important to note that any exception thrown here will cause the type to be no longer available until the appdoamin is reloaded, so be careful, and there are two types of deadlocks that can easily occur in the static contructor. Be sure to think clearly when writing code.



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.