C # BASICS (7) -- differences between static and non-static classes and static members

Source: Internet
Author: User

Static class

An important difference between static and non-static classes is that static classes cannot be instantiated.That is to say, you cannot use the new keyword to create a variable of the static class type. When declaring a class, using the static keyword has two meanings: First, it prevents programmers from writing code to instantiate the static class; second, it prevents any instance fields or methods from being declared within the class.

Static classes are introduced only from C #2.0. C #1.0 does not support static class declarations.. The programmer must declare a private constructor. The private constructor prohibits developers from instantiating class instances outside the class scope. The effect of using a private constructor is very similar to that of using a static class. The difference between the two lies in that the private constructor can still instantiate the class from inside the class, while the static class cannot be instantiated from anywhere, including from inside the class. Another difference between a static class and a private constructor is that an instance member is allowed in a class that uses a private constructor, C #2.0 and later compilers do not allow static classes to have any instance members.The advantage of using static classes is that the compiler can perform checks to ensure that instance members are not accidentally added, and the compiler will ensure that such instances are not created.Another feature of a static class is that the C # compiler automatically marks it as sealed. This keyword specifies a class as non-extensible; in other words, it cannot derive other classes from it.


Main features of static classes:
1: contains only static members.
2: it cannot be instantiated.
3: it is sealed.
4: The instance constructor cannot be included.


Static Member

1: non-static classes can contain static methods, fields, attributes, or events;

2: no matter how many instances are created for a class, its static members have only one copy;

3: static methods and attributes cannot access non-static fields and events of their contained types, and cannot access instance variables of any objects;
4: static methods can only be overloaded, but cannot be overwritten, because static methods do not belong to instance members of the class;
5: although the field cannot be declared as static constThe behavior of the const field is static in nature.. Such fields belong to the class,

Does not belong to the class instance. Therefore, you can access the const field using classname. membername notation like static fields. 6: C # does not support static local variables (static variables are defined within the method ).

 

Static Constructor

1: static classes can have static constructor, and static constructor cannot inherit;

2: static constructors can be used for static classes or non-static classes;

3: The static constructor has no access modifier or parameter and only one static flag;

4: The static constructor cannot be called directly. When a class instance is created or any static member is referenced, the static constructor is automatically executed and

Run only once.

 

Note:

1: static classes are always located in the memory;

2: non-static classes are independent in the memory after being instantiated. Their variables will not be repeated and will be destroyed in time after use, so there will be no Unknown

.In C #, static members are sensitive and should not be used unless otherwise confirmed.;

3: We recommend that you use more general classes (non-static classes ).

 

Usage selection:

When the defined class does not need to be instantiated, we use static classes. If you need to instantiate an object and inherit from other features, you should use non-static classes, and set the variables and methods used in a unified manner to static, so that all instance objects can be accessed.

C # BASICS (7) -- differences between static and non-static classes and static members

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.