C # static class static and non-static methods comparison

Source: Internet
Author: User

Static class

Adding a static decoration to the class (classes) means that the class cannot be instantiated and that the variable or function cannot be instantiated in that class

Key features of static classes

    • Include only static members
    • Cannot instantiate
    • The nature of a static class, when an abstract sealed class, cannot be inherited
    • Cannot contain instance constructors
    • This class can be defined as a static class if the members under a class need to be shared.

Therefore, you can add static methods to a normal class, but methods and variables in a static class must be static.

C # static and non-static methods comparison

Static method: Static method is the method to modify with static, otherwise it is non-static method.

Two

(1) A static member belongs to a class, so a non-static member belongs to an instance of the class.

(2) Each instance of a class is created, a new chunk of storage is allocated in memory for non-static members

Summary: A static member belongs to a class so, for instance of each class, no matter how many instances the class creates, the static members of the class occupy only one piece of memory.

Three

(1) C # static methods belong to a class so you can use it before the class is instantiated

(2) Non-static methods can access any member of the class, and static methods can only access static members in the class

(3) Because static methods are available before the class is instantiated, non-static variables in the class must be instantiated before the memory can be allocated. In this way, C # static method calls cannot determine the memory address used by non-static variables, so static methods cannot use non-static variables, but static variable addresses are fixed for classes, so static methods can use static variables.

(4) C # static method is a special member method, which does not belong to a specific instance of a class, but belongs to the class itself. Therefore, for static methods, you do not need to create an instance of a class first, but instead use the class name. The format of the static method.

The 1.static method is a member method in a class that belongs to the entire class, that is, you can call it directly without creating any objects!

2. Static methods are more efficient than instancing, and the disadvantage of static methods is that they are not destroyed automatically, while instantiated can be destroyed.

3. Static and static variables always use the same piece of memory after they are created, and the way the instance is used creates multiple memory.

There are two methods in 4.c#: Instance method, static method.

Four
Get the name of a class in C # static method
Static methods:
String className = System.Reflection.MethodBase.GetCurrentMethod (). Reflectedtype.fullname;

Non-static methods can also be used:
String className = this. GetType (). FullName;

The difference between C # static and non-static methods is interpreted to explain the meaning of C # static methods, and it is hoped to help you understand and learn C # static and non-static methods.

C # static class static and non-static methods comparison

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.