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. }}
A simple program of C language Bubble Sorting
Main ()
{
Int I, j, temp;
Int a [10];
For (I = 0; I <10; I ++)
Scanf ("% d,", & a [I]);
For (j = 0; j <= 9; j ++)
{For (I = 0; I <10-j; I ++)
If (a [I]> a [I + 1])
{Temp = a [I];
A [I] = a [I + 1];
A [I + 1] = temp ;}
}
For (I = 1; I <11; I ++)
Printf ("% 5d,", a [I]);
Printf ("\ n ");
}
--------------
Bubble Algorithm
Algorithm Analysis and Improvement of Bubble Sorting
The basic idea of exchanging sorting is to compare the keywords of the records to be sorted in pairs. If the order of the two records is the opposite, the two records are exchanged until there is no reverse order record.
The basic concepts of application exchange sorting include Bubble sorting and quick sorting.
Bubble Sorting
1. Sorting Method
Vertically arrange the sorted record array R [1. n]. Each record R is considered as a bubble with the weight of R. key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.
(1) initial
R [1. n] is an unordered area.
(2) First scan
The weights of two adjacent bubbles are compared from the bottom of the unordered area to the top. If the light bubbles are found to be in the lower and severe bubbles, the positions of the two bubbles are exchanged. That is, compare (R [n], R [n-1]), (R [n-1], R [N-2]),…, (R [2], R [1]); for each pair of bubbles (R [j + 1], R [j]), if R [j + 1]. key <R [j]. key, then the contents of R [j + 1] and R [j] are exchanged.
When the first scan is complete, the "lightest" bubble floated to the top of the interval, that is, the record with the smallest keyword is placed on the highest position R [1.
(3) second scan
Scan R [2. n]. When scanning is completed, the "light" bubble floated to the R [2] position ......
Finally, the sequential area R [1. n] can be obtained through n-1 scanning.
Note:
During the I-trip scan, R [1 .. I-1] and R [I.. n] are the current sequential and disordered areas, respectively. The scan continues from the bottom of the unordered area to the top of the area. When scanning is completed, the shortest bubbles in the area float to the top position R. The result is that R [1. I] is changed to a new ordered area.
2. Bubble sorting process example
Bubble Sorting of files whose keyword sequence is 49 38 65 97 76 13 27 49
3. Sorting Algorithm
(1) Analysis
Because each sort adds a bubble to the ordered area, there are n-1 bubbles in the ordered area after N-1 sort, in the disordered area, the bubble weight is always greater than or equal to the bubble weight in the ordered area. Therefore, the entire Bubble sorting process requires at most n-1 sorting.
If no bubble position exchange is found in a sorting, it means that all bubbles in the unordered area to be sorted meet the principle of being light and heavy. Therefore, the Bubble sorting process can be terminated after this sorting. Therefore, in the following algorithm, a Boolean exchange is introduced, which is set to FALSE before each sort starts. If an exchange occurs during the sorting process, set it to TRUE. Check exchange at the end of sorting. If exchange has not occurred, terminate the algorithm and no longer perform the next sorting.
(2) specific algorithms
Void BubbleSort (SeqList R)
{// R (l. n) is the file to be sorted. It uses bottom-up scanning to perform Bubble Sorting on R.
Int I, j;
Boolean exchange; // exchange flag
For (I = 1; I <G id = "1">
A simple program of C language Bubble Sorting
Main ()
{
Int I, j, temp;
Int a [10];
For (I = 0; I <10; I ++)
Scanf ("% d,", & a [I]);
For (j = 0; j <= 9; j ++)
{For (I = 0; I <10-j; I ++)
If (a [I]> a [I + 1])
{Temp = a [I];
A [I] = a [I + 1];
A [I + 1] = temp ;}
}
For (I = 1; I <11; I ++)
Printf ("% 5d,", a [I]);
Printf ("\ n ");
}
--------------
Bubble Algorithm
Algorithm Analysis and Improvement of Bubble Sorting
The basic idea of exchanging sorting is to compare the keywords of the records to be sorted in pairs. If the order of the two records is the opposite, the two records are exchanged until there is no reverse order record.
The basic concepts of application exchange sorting include Bubble sorting and quick sorting.
Bubble Sorting
1. Sorting Method
Vertically arrange the sorted record array R [1. n]. Each record R is considered as a bubble with the weight of R. key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.
(1) initial
R [1. n] is an unordered area.
(2) First scan
The weights of two adjacent bubbles are compared from the bottom of the unordered area to the top. If the light bubbles are found to be in the lower and severe bubbles, the positions of the two bubbles are exchanged. That is, compare (R [n], R [n-1]), (R [n-1], R [N-2]),…, (R [2], R [1]); for each pair of bubbles (R [j + 1], R [j]), if R [j + 1]. key <R [j]. key, then the contents of R [j + 1] and R [j] are exchanged.
When the first scan is complete, the "lightest" bubble floated to the top of the interval, that is, the record with the smallest keyword is placed on the highest position R [1.
(3) second scan
Scan R [2. n]. When scanning is completed, the "light" bubble floated to the R [2] position ......
Finally, the sequential area R [1. n] can be obtained through n-1 scanning.
Note:
During the I-trip scan, R [1 .. I-1] and R [I.. n] are the current sequential and disordered areas, respectively. The scan continues from the bottom of the unordered area to the top of the area. When scanning is completed, the shortest bubbles in the area float to the top position R. The result is that R [1. I] is changed to a new ordered area.
2. Bubble sorting process example
Bubble Sorting of files whose keyword sequence is 49 38 65 97 76 13 27 49
3. Sorting Algorithm
(1) Analysis
Because each sort adds a bubble to the ordered area, there are n-1 bubbles in the ordered area after N-1 sort, in the disordered area, the bubble weight is always greater than or equal to the bubble weight in the ordered area. Therefore, the entire Bubble sorting process requires at most n-1 sorting.
If no bubble position exchange is found in a sorting, it means that all bubbles in the unordered area to be sorted meet the principle of being light and heavy. Therefore, the Bubble sorting process can be terminated after this sorting. Therefore, in the following algorithm, a Boolean exchange is introduced, which is set to FALSE before each sort starts. If an exchange occurs during the sorting process, set it to TRUE. Check exchange at the end of sorting. If exchange has not occurred, terminate the algorithm and no longer perform the next sorting.
(2) specific algorithms
Void BubbleSort (SeqList R)
{// R (l. n) is the file to be sorted. It uses bottom-up scanning to perform Bubble Sorting on R.
Int I, j;
Boolean exchange; // exchange flag
For (I = 1; I <G id = "1">