C #2.0 static class [2]

Source: Internet
Author: User
Tags abstract static class
Let's take a look at what static class is. A long time ago, I was so worried that I couldn't declare an abstract sealed class. I raised this question on CSDN, but it didn't resonate. The reply I got is as follows:
Abstract used together with sealed? The purpose of defining abstract objects is to abstract the base class. seal indicates the entity classes that cannot be inherited. There are two different things. How can they be used together?
Abstract indicates that the object must be inherited, and sealed indicates that the object cannot be inherited. These two objects are in conflict. How can they be used together.
By viewing the IL code, we will find that static class is actually abstract sealed class, but the compiler will check the member modifier of static class at compile time.
Let's guess what MS developers think: their purpose is to get the abstract sealed class effect, but it is worried about semantic contradictions, we wanted to get a beautiful solution, so we added a static keyword for C #2.0, which perfectly solved this problem.
Some may ask: why is the sealed constraint not added for static class = abstract class + static member limit? The reason is that there is no difference between inheriting a class with only static members and re-writing a class. Why? Because static is not virtual, what can be used to inherit it?
Static class has another restriction, that is, it can only inherit from System. Object. Why is this restriction, for the same reason as above.
Every time I mention the static keyword, I think of static constructor, thanks to the developers of MS for providing such a good feature. However, C # provides static constructor but does not provide static destructor. in the NET Framework Programming Book, Jeffery Richter provides us with a way to use System. appDomain. the DomainUnload event achieves the same effect.
When using static constructor, note that any exception thrown here will cause this type to no longer be available until AppDoamin is Reloaded. Therefore, be careful, in addition, static contructor is prone to two types of deadlocks, which must be clear when writing code.

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.