8.3 C# constants and read-only fields (Java final variables )In Java, a final variable is one whose value cannot be reassigned once it has been given a value. You can only use a final variable after it has been assigned a value “ all final variables must be assigned values at some point in time before its first use, otherwise the compiler complains. In Java: 1: // FinalTest.java 2: public class FinalTest{ 3: final int FINAL_VAR; 4: 5: public FinalTest(int newValue){ 6: FINAL_VAR = newValue; // compilation error if omitted. 7: } 8: 9: public static void main(String args[]){ 10: FinalTest f = new FinalTest(4); 11: } 12: } If FINAL_VAR has been declared with the static modifier too on line 3, you will have to either:
This is because you cannot assign a value to a static variable from outside a static context, and the constructor coded above (lines 5 “ 7) is an instance method. In C#, instead of final variables, there are C# constants, and read-only fields. [4]
8.3.1 C# constantsA C# constant is like a final variable which must be initialized in the same declaration statement. What this means is that you cannot declare a constant and initialize it in a separate statement. You use the C# const keyword to declare a constant. Unlike Java
Like JavaA constant is similar to a Java final static variable except that it must be initialized in the same statement as the one that it has been declared in. 1: using System; 2: class TestClass{ 3: const int FinalVar = 5; 4: 5: public static void Main(){ 6: Console.WriteLine(FinalVar); 7: } 8: } In the code above, you can use TestClass.FinalVar instead of FinalVar within the class itself (on line 6), since all constants are implicitly static members of the class. 8.3.2 C# read-only fieldsThe main difference between a C# constant and a C# read-only field is that a read-only field can be initialized in a separate statement (in a constructor) from the one in which it is declared. [6] Hence, unlike a constant, a read-only field's value can be determined at runtime, as long as the read-only field is initialized before it is used.
Direct assignments to read-only fields can only occur:
A read-only field can be reassigned a new value any number of times, as long as the condition above is satisfied. Read-only fields are generally used instead of constants if the value of that field cannot be determined before runtime. Use the C# readonly keyword to declare a read-only variable: 1: class TestReadOnly{ 2: readonly int FinalVar; 3: 4: TestReadOnly(int newValue){ 5: // assignment in an instance constructor 6: FinalVar = newValue; 7: } 8: 9: public static void Main(){ 10: TestReadOnly tro = new TestReadOnly(9); 11: System.Console.WriteLine(tro.FinalVar); 12: } 13: } Output: c:\expt>test 9 The code below produces a compilation error because an attempt is made to assign FinalVar with a value outside a constructor . 1: class TestReadOnly{ 2: readonly int FinalVar; 3: 4: public static void Main(){ 5: TestReadOnly tro = new TestReadOnly(); 6: tro.FinalVar = 1; // illegal assignment 7: } 8: } Compilation error: [View full width]
You can assign a value to a read-only variable when it is being declared (as part of the variable initializer). For example, you can replace line 2 with: 2: readonly int FinalVar = 9 ; Additional Notes
|