Define a real read-only List in C,
The readonly keyword in C # indicates that fields in the class can only be initialized during definition or in the constructor. Normal data can achieve the expected results, but in the object or list, to achieve the read-only effect, only one readonly keyword is not allowed. When you modify a List with readonly, you can still use the Add and Remove methods in other classes to change it. However, the read-only attribute you want may be:Only items in this list can be modified in the current class!
The following example shows a List modified with the readonly keyword. Its content items can still be added or deleted in other classes:
Even if it is encapsulated as a read-only attribute, you can still operate in other classes:
However, you can change the attribute type to IEnumerable <T> to achieve the desired effect. Because the IEnumerable <T> class does not have the Add and Remove methods, the List <T> class inherits the IEnumerable <T> class, And the Add and Remove methods added to the List <T> class are as follows:
If you want to create a fully read-only attribute, you can use ReadOnlyCollection even if it cannot be modified in the current class. <T>:
In. NET 4.5, List <T> inherits IReadOnlyList <T> and IReadOnlyCollection <T>, giving us a simpler way of writing, which can also achieve the above results:
In C language <yes?
First, move left. To move left is to move all the bits of a number to the left. In C, the <operator is used. For example:
Int I = 1;
I = I <2; // shifts the value in I two places to the left.
That is to say, the binary system of 1 is 000... 0001 (here, the number of 0 in front of 1 is related to the number of digits in int, 32-bit machine, 31 0 in gcc), shifted to 000 after 2 digits left... 0100, that is, 4 in decimal system. Therefore, if the Left shift of 1 is equivalent to multiplying by 2, then the left shift of n is multiplied by the Npower of 2 (the number of symbols is not fully applicable, this is because the left shift may cause symbol changes. The reasons are described below)
One problem that needs to be noted is the symbol bit at the leftmost end of the int type and the shift out. we know that int Is the signed integer number, and the first bit on the leftmost side is the signed bit, that is, 0 is positive and 1 is negative. Then, overflow occurs during shift. For example:
Int I = 0x40000000; // 40000000 of hexadecimal notation, 01000000 of hexadecimal notation... 0000
I = I <1;
Then, after I shifts 1 to the left, it will become 0x80000000, that is, 100000 of the binary system... 0000, the sign bit is set to 1, and the other bits are all 0, which is changed to the minimum value that can be expressed by the int type. The 32-bit int value is-2147483648, overflow. what will happen if I is moved 1 to the left? In C language, the highest bit is discarded. After 1 is discarded, the value of I is changed to 0.
A special case in the left shift is that when the number of digits in the left shift exceeds the maximum number of digits in the value type, the compiler will use the number of digits in the left shift to de-model the maximum number of digits and then shift by the remainder, for example:
Int I = 1, j = 0x80000000; // set int to 32 characters
I = I <33; // 33% 32 = 1 shifted to 1, and I changed to 2
J = j <33; // 33% 32 = 1 shifts 1 bit left, j changes to 0, and the highest bit is discarded.
When compiling this program with gcc, the compiler will provide a warning, indicating the number of places to move left> = type length. in fact, I, j moves the remainder of 1 bit, that is, 33% after 32. this rule is implemented in gcc. It is unclear whether other compilers are the same.
In short, the Left shift is: discard the highest bit, 0 fill the second bit
Now that the right shift is clear, the right shift is better understood.
The concept of right shifting is opposite to that of left shifting. It is to move several digits to the right. The operator is>.
The bitwise of the right-shift symbol is different from that of the left-shift symbol. For signed integers, for example, int type, the right-shift symbol remains unchanged. For example:
Int I = 0x80000000;
I = I> 1; // The I value will not change to 0x40000000, but 0xc0000000
That is to say, after the sign bit moves to the right, if it is positive, it fills in 0, and if it is negative, it fills in 1, that is, the arithmetic shift in assembly language to the right. similarly, when the number of digits to be moved exceeds the length of the type, the remainder is obtained and then the remainder is moved.
Negative number 10100110> 5 (assuming the word length is 8 characters), the result is 11111101.
In short, in C, the Left shift is the logical/arithmetic left shift (the two are identical), and the right shift is the arithmetic right shift, which will keep the symbol bit unchanged. in practical applications, you can use the left/right shift to perform fast multiplication/Division operations, which is much more efficient than loop operations.
For example, in C language, the Left shift <represents multiplied by 2, right shift> represents dividing by 2, which is caused by how the computer works! But if it is 7, the binary number is 0111, And the right shift is 3.5, but after the right shift, the binary number is 0011, which is 3. Different. How can I explain it ??
A: The two operands of the shift operator must be integer. The Value Type of the entire shift expression is also integer, and the rest of the full text>
+ = What does C mean?
For example, a + = B is equivalent to a = a + B, that is, the value of a + B is assigned to.
Do you understand?
Contact me