Static classes and non-static classes

Source: Internet
Author: User

Static classes are basically the same as non-static classes, but there is one difference: Static classes cannot be instantiated.In other words, you cannot use the New keyword to create a variable of a static class type. because there is no instance variable, you use the class name itself to access the members of the static class. For example, if a static class named Utilityclass has a public method named MethodA, the method is called as shown in the following example:

C#
Utilityclass.methoda ();

Static classes can be conveniently used as containers for these method sets for methods that only operate on input parameters and do not get or set any internal instance fields.For example, in the. NET Framework class Library, the static class System. Math contains methods that perform only mathematical operations, without the need to store or retrieve data specific to a particular instance of the math class. That is, the class member is applied by specifying the class name and the method name, as the following example describes.

Double dub =-3.14; Console.WriteLine (Math.Abs (Dub)); Console.WriteLine (Math.floor (Dub)); Console.WriteLine (Math.Round (Math.Abs (Dub)));//output://3.14// -4//3

As with all class types, when you load a program that references a static class, the. NET Framework Common Language Runtime (CLR) loads the type information for that static class. The program cannot specify the exact time to load a static class.However, it is guaranteed to load the class before it is first referenced in the program, initialize the field of the class, and call its static constructor. Static constructors are called only once, and static classes remain in memory for the lifetime of the application domain in which the program resides.

The following table describes the main characteristics of a static class:

    • contains only static members.

    • cannot be instantiated.

    • is sealed.

    • instance constructors. " > cannot contain instance constructors.

Therefore, creating a static class is basically the same as creating a class that contains only static members and private constructors. The private constructor prevents the class from being instantiated. the advantage of using static classes is that the compiler is able to perform checks to ensure that instance members are not accidentally added. The compiler will guarantee that instances of this class will not be created.

Static classes are sealed and therefore cannot be inherited.They cannot inherit from any class except for Object. a static class cannot contain an instance constructor, but it can contain a static constructor. if a non-static class contains static members that require important initialization, you should also define a static constructor.

A non-static class can contain static methods, fields, properties, or events.Even if you do not create an instance of the class, you can call a static member in that class.static members are always accessed through the class name, not the instance name. no matter how many instances are created on a class, its static members have only one copy. static methods and properties cannot access non-static fields and events in their containing types, and cannot access the instance variables of any object (unless explicitly passed in method parameters).

It is more common to declare a non-static class with some static members, rather than declaring the entire class as a static class. static fields have two common uses: one is to record the number of objects that have been instantiated, and the other is to store values that must be shared across all instances.

Static methods can be overloaded but cannot be overridden because they belong to a class and do not belong to any instance of the class.

Although a field cannot be declared asStatic const, but the behavior of the Const field is essentially static. such a field belongs to a type and is not an instance of a type. Therefore, you can use classname.membername notation to access the Const field as you would with a static field. object instances are not required.

C # does not support static local variables (variables declared within the scope of a method).

You can declare a static class member by using the static keyword before the return type of the member, as shown in the following example:

C#
public class automobile{public    static int numberofwheels = 4;    public static int Sizeofgastank    {        get        {            return;        }    }    public static void Drive () {} public    static event EventType Runoutofgas;    Other non-static fields and properties ...}

Static members are initialized before they are first accessed and before a static constructor is called, if there is one. to access a static class member, you use the class name instead of the variable name to specify the location of the member, as shown in the following example:

C#
Automobile.drive (); int i = automobile.numberofwheels;

If your class contains static fields, provide a static constructor that initializes these fields when the class is loaded.

A call to a static method generates a call instruction in Microsoft intermediate language (MSIL), whereas a call to an instance method generates a callvirt instruction that also checks for a null object reference. However, the performance difference between the two is not obvious for most of the time.

Static classes and non-static classes

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.