Guide to the Language


Download 2 Mb.
Pdf ko'rish
bet73/78
Sana30.04.2023
Hajmi2 Mb.
#1414515
TuriGuide
1   ...   70   71   72   73   74   75   76   77   78
Bog'liq
C sharp

CHAPTER 29
Constants
A variable in C# can be made into a compile-time constant by adding the 
const keyword before the data type. This modifier means that the variable 
cannot be changed and it must therefore be assigned a value at the same 
time as it is declared. Any attempts to assign a new value to the constant 
will result in a compile-time error.
 Local Constants
A local constant must always be initialized at the same time as it is 
declared.
static void Main()
{
const int a = 10; // compile-time constant
}
The const modifier creates a compile-time constant, and so the 
compiler will replace all usage of the constant with its value. The assigned 
value must therefore be known at compile time. As a result of this, the 
const modifier may only be used together with the simple types, as well as 
with enum and string types.


172
 Constant Fields
The const modifier can be applied to a field to make the field 
unchangeable.
class MyClass
{
const int b = 5; // compile-time constant field
}
Constant fields cannot have the static modifier. They are implicitly 
static and are accessed in the same way as static fields.
int a = MyClass.b;
 Readonly
Another variable modifier similar to const is readonly, which creates a 
runtime constant. This modifier can be applied to fields, and like const, it 
makes the field unchangeable.
class MyClass
{
readonly int c = 3; // run-time constant field
}
Since a readonly field is assigned at runtime, it can be assigned a 
dynamic value that is not known until runtime.
readonly int d = System.DateTime.Now.Hour;
Unlike const, readonly can be applied to any data type.
readonly int[] e = { 1, 2, 3 }; // readonly array
Chapter 29 Constants


173
Additionally, a readonly field cannot only be initialized when it is 
declared. It can also be assigned a value in the constructor.
class MyClass
{
readonly string s;
public MyClass() { s = "Hello World"; }
}
As of C# 7.2, the readonly modifier can be applied to not just fields but 
also to structs. Declaring a struct as readonly will enforce immutability 
on the members of the struct, requiring all fields and properties to be 
made readonly.
readonly struct MyStruct
{
public readonly int myVar;
public int myProperty { get; }
public MyStruct(int var, int prop)
{
myVar = var;
myProperty = prop;
}
}
Another addition in C# 7.2 is the ability to mark a method’s return 
value as readonly when returning a value type by reference with the ref 
modifier. This will disallow the caller from modifying the returned value, 
provided that the returned value is also assigned as a readonly reference 
and not just a copy.
Chapter 29 Constants


174
class MyClass
{
readonly static int i;
static ref readonly int GetValue() { return ref i; }
static void Main()
{
ref readonly int a = ref GetValue();
a = 5; // error: readonly variable
}
}

Download 2 Mb.

Do'stlaringiz bilan baham:
1   ...   70   71   72   73   74   75   76   77   78




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling