The similarities and differences between static and non-static in. NET

Source: Internet
Author: User

For beginners ,. NET static and non-static has been relatively difficult to grasp, here is a summary, introduce static class and common class, static method and instance method, static constructor and instance constructor, the difference between static and non-static fields.

Static vs common

1) Static classes cannot be instantiated, but common classes can;
2) static classes can only inherit from the System. Object base class. normal classes can inherit from any other non-static and non-sealed classes, but only one class can be inherited;
3) Static classes cannot inherit interfaces. Common classes can inherit multiple interfaces;
4) Static classes can only contain static members. Common classes can contain static and non-static members;
5) Static classes cannot be used as fields, method parameters and local variables; common classes can;

Static classes can be used to encapsulate a group of methods that are not associated with any objects, such as Math and Console classes.

Static constructor vs instance Constructor

1) The static constructor belongs to the class. It is executed only once when the class is used for the first time. The instance constructor belongs to the object and will be executed every time a new object is initialized;
2) A static constructor can be defined only once and cannot contain parameters. An Instance constructor can define overload and include parameters;
3) The static constructor can only access static fields of the type. The instance constructor can access static and non-static fields of the type;
4) Static classes cannot contain access modifiers. The default value is private.

Set the initialization of the type, such as the Instance Object required for the initialization type, and assign values to the static field of the type.

Static vs instance

1) Static Methods belong to Classes and are called through classes; instance methods belong to objects and are called through objects;
2) static methods cannot be non-static members of the callback class;

Complete a function unrelated to a specific object.

Static Field vs non-static field

Static fields belong to the class and are called through the class. Non-static fields belong to the object and are called through the object.

You can use static fields to record information about the class itself.

Via 51cto

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.