Differences between static and non-static in. NET

Source: Internet
Author: User

Static vs common

The differences between static classes and common classes are as follows:

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;

Significance of static classes:

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

The differences between a static constructor and an instance constructor are as follows:

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.

Significance of static constructor:

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

The differences between static methods and instance methods are as follows:

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;

Significance of static methods:

Complete a function unrelated to a specific object.

Static Field vs non-static field

Differences between static fields and non-static fields:

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.

Significance of static fields:

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

Code demo

Copy codeThe Code is as follows: public class Test
{
Public int I = 10;
Public static int j = 20;
Public int k;

Public Test ()
{
Console. WriteLine ("I is a non-static field, its value is {0}", I );
Console. WriteLine ("j is a static field, its value is {0}", j );
}

Public Test (int k)
{
This. k = k;
Console. WriteLine ("I is a non-static field, its value is {0}", I );
Console. WriteLine ("j is a static field, its value is {0}", j );
Console. WriteLine ("k is a non-static field, its value is {0}", k );
}

Static Test ()
{
Console. WriteLine ("I am a static constructor, I couldn't contain any parameters! ");
Console. writeLine ("I couldn't access to the non-static field I, I can only access to the static field j, the value of j is {0}", j );
}

Public void Print ()
{
Console. WriteLine ("I am a instance method, I can access both the non-static field and the static field! ");
Console. WriteLine ("The value of I is {0} and the value of j is {1}", I, j );
}

Public static void StaticPrint ()
{
Console. writeLine ("I am a static method, I couldnt access to the non-static field I, I can only access to the static field j, the value of j is {0 }", j );
}
}

Running result


From the running results, we can see that two Test objects are created in the Main method, and the static constructor is executed only once and prior to the instance constructor, the instance constructor executes each time during the two instantiation processes. At the same time, we can see that we call the j field through Test. j, and the call of the I field and k field is called through the two objects t1 and t2 of Test. Similarly, the static method StaticPrint is also called using the Test class, while the instance method Print is called using the Test class object.

Fighting like Allen Iverson! Never never give up!

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.