Static class and Static class members

Source: Internet
Author: User

Static and non-static classes are basically the same, but there is one difference: Static classes cannot be instantiated.

Key features of static classes:

    • Contains only static members.
    • cannot be instantiated.
    • is sealed.
    • Cannot contain instance constructors.

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. No matter how many instances are created on a class, its 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 fields are types, not instances of types. classname.membername notation that's used for static field S. " As a result, you can use classname.membername notation to access the Const field as you would for a static field. object instances are not required.

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

Static members are initialized before they are accessed for the first time and before a static constructor is called, if there is one.

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

Static class and Static class members

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.