Seven Questions C # keywords const and readonly,
Const and readonly are often used to modify fields of the class. What are the similarities and differences between them?
Const
1. Do I have to assign an initial value to declare a const type variable?
-- The initial value must be assigned.
public class Student { public const int age; }
The following error is reported during generation:
The correct statement should be as follows:
public class Student { public const int age = 18; }
2. Can I declare a const type variable with static modification?
-- No
public class Student { public static const int age = 18; }
The following error is reported during generation:
The correct statement should be as follows:
public class Student { public const int age = 18; }
Because const is static by default.
3. Can a runtime variable be assigned to a const type variable?
-- No
public class Student { public const int age = 18; public Student(int a) { age = a + 1; } }
The following error is reported during generation:
Const type variables are compile-time variables and cannot be assigned to runtime variables.
4. Can const modify referenced type variables?
-- Yes, but only null values can be assigned to the reference type variable.
public class Student { public const Teacher teacher = new Teacher(); } public class Teacher { }
The following error is reported during generation:
The correct statement should be as follows:
public class Student { public const Teacher teacher = null; } public class Teacher { }
Readonly
5. Do I have to assign an initial value to declare a readonly variable?
-- Not necessarily, either an initial value or an initial value can be assigned.
The following statements do not include initial values:
public class Student { public readonly int age; }
The following initial values are also written:
public class Student { public readonly int age = 18; }
6. Can a runtime variable be assigned to a readonly variable?
-- Yes
The following constructor can assign values to the readonly type variable:
public class Student { public readonly int age = 18; public Student(int a) { age = a; } }
7. Can I declare a variable of the readonly type with static modification?
-- Yes
The following statements are correct:
public class Student { public static readonly int age = 18; }
Summary
Const modifier:
● The variable modified with const is the variable during compilation.
● The runtime variable cannot be assigned to the variable modified by const.
● The variable modified by const must be declared with an initial value.
● The variable modified by const cannot be modified by static.
● Cosnt can also modify the reference type variable, but must assign the null initial value to the Reference Type Variable
Readonly modifier:
● The variable modified with readonly is a runtime variable.
● You can assign the runtime variable to the variable modified by readonly.
● When a variable modified by readonly is declared, the initial value can be assigned or not assigned.
● The variable modified by readonly can be preceded by a static modifier.
7. Problem Solving Process
A
I only asked for the answer to the seventh question,
Add a proper amount of calcium hydroxide Ca (OH) 2 + NaOH = calcium carbonate mass + NaOH, then filter and precipitate and then evaporate the solution to obtain a pure sodium hydroxide solid.
The caustic soda sample contains the impurity sodium carbonate how to remove sodium carbonate to obtain a relatively pure sodium hydroxide solid, the specific operation process and to achieve the purpose.
--- A chemical question: Industrial caustic soda 2g, made into 50 ml mL solution, take can be completely neutralization with 0.2mol/L 20 mL sulfuric acid, to find the purity of caustic soda?
Solution: n (sulfuric acid) = 0.2mol/L * 20 mL = 0.004mol
If the NaOH reacted with sulfuric acid is xmol, then:
Sulfuric acid + 2 NaOH = na2sulfate + 2H2O
1 2
0.004mol xmol
Well: 1/0.004mol = 2/xmol
Required: x = 0.008
Total amount of NaOH contained in 2G caustic soda samples = 0.008mol * 5 = 0.04mol
M (NaOH) = 0.04mol * 40g/mol = 1.6g
Caustic Soda purity = 1.6g/2g * 100% = 80%
A: The purity of caustic soda is 80%.