C #-Static members

Source: Internet
Author: User

A Non-static class can contain static methods, fields, properties, or events. The static member is callable in a class even when no instance of the class have been created. The static member is all accessed by the class name and not the instance name. Only one copy of a static member exists, regardless of what many instances of the class is created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access An instance variable of any object unless it was explicitly passed in a method parameter.

It is more typical to declare a non-static class with some static members, than to declare an entire class as static. Common uses of static fields is to keep a count of the number of objects that has been instantiated, or to store a V Alue that must is shared among all instances.

Static methods can overloaded but not overridden, because they belong to the class, and not to any instance of the Clas S.

Although a field cannot be declared asstatic const, a const field is essentially static in its behavior. It belongs to the type, not to instances of the type. Therefore, const fields can being accessed by using the sameClassname.membername notation that's used for static fi ELDs. No object instance is required.

C # does not the support static local variables (variables is declared in method scope).

Static members was initialized before the static member is accessed for the first time and before the static constructor, If there is a, is called.

If your class contains static fields, provide a static constructor then initializes them 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 INS Tance method generates acallvirt instruction, which also checks for a null object references. However, the most of the time the performance difference between the and the "not" significant.

C #-Static 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.