C # static usage,

Source: Internet
Author: User

C # static usage,

I. Static class

The important difference between static classes and non-static classes is that static classes cannot be instantiated, that is, they cannot use the new keyword to create static class type variables. When declaring a class, using the static keyword has two meanings: First, it prevents programmers from writing code to instantiate the static class; second, it prevents any instance fields or methods from being declared within the class.

Main features of static classes:


1: contains only static members.


2: it cannot be instantiated.
 

3: the essence of static classes is an abstract and sealed class. Therefore, they cannot be inherited or instantiated.

4: The instance constructor cannot be included.

5: if all the members under a class need to be shared, you can define this class as a static class.

 

Differences between static classes and private Constructors

 

1. The private constructor can still instantiate classes from inside the class, while static classes are prohibited from instantiating classes from anywhere, including from inside the class.

2. In a class using a private constructor, instance members are allowed, and the compiler does not allow static classes to have any instance members.

3. The advantage of using a static class is that the compiler can perform checks to ensure that instance members are not accidentally added, and the compiler will ensure that instances of this class are not created.

4. the C # compiler automatically marks it as sealed. This keyword specifies a class as non-extensible; in other words, it cannot derive other classes from it.

 

Ii. Static variables

1. Modified by the static keyword. It belongs to the class and instance members belong to the object. When this class is loaded for the first time, all static members under this class will be loaded.

2. A static member is created only once. Therefore, there is only one static member, and there are only one copy of each member.

3. When a class is loaded, all static members are created inStatic storage Zone", Once created until the program exits, it will be recycled.

4. When variables need to be shared and methods need to be called repeatedly, these members can be defined as static members.

5. In static methods, instance members cannot be called directly, because objects may not exist when static methods are called.

6. this/base keywords cannot be used in static methods, because the object may not exist.

7. You can create an object of this class and specify that the object members operate in static methods.

8. You can call static members in the instance method, because static members certainly exist at this time.

9. Non-static classes can contain static methods, fields, attributes, or events;
10. No matter how many instances are created for a class, its static members have only one copy;
11. Static methods and attributes cannot access non-static fields and events of their contained types, and cannot access instance variables of any objects;
12. Static methods can only be overloaded, but cannot be overwritten, because static methods do not belong to instance members of the class;
13. Although the field cannot be declared as static const, the behavior of the const field is static in nature. Such fields belong to the class and do not belong to the instance of the class.

 

Iii. Static Methods

Static methods are methods that do not belong to a specific object,

Static methods can access static member variables,

Static methods do not allow direct access to instance variables. You can pass the instance variable volume as a parameter to the static method when the instance function is called.

Static methods cannot directly call instance methods. They can be called indirectly. You must first create an instance of the class and then call static methods through this specific object.

 

Iv. Static Constructor


1: static classes can have static constructor, and static constructor cannot inherit;
2: static constructors can be used for static classes or non-static classes;
3: The static constructor has no access modifier or parameter and only one static flag;
4: The static constructor cannot be called directly. When a class instance is created or any static member is referenced, the static constructor is automatically executed and only executed once.

For example

C # code Replication
Class Program {public static int I = 0; public Program () {I = 1; Console. write ("instance constructor called");} static Program () {I = 2; Console. write ("static constructor executed");} static void Main (string [] args) {Console. write (Program. i); // The result is 2. First, the class is loaded, all static members are created in the static storage area, I = 0, and then the class members are called, at this time, the static constructor will be called, I = 2 Program p = new Program (); Console. write (Program. i); // The result is 1. After strength, the instance constructor is called. I = 1. Because the static constructor is executed only once, it will not be executed again. }}

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

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.