C # static readonly const usage

Source: Internet
Author: User

Const constant
1. it can be used to modify fields or local variables of a class. the variable modified by const can only be a value type, not a reference type. in fact, it can also modify the reference type, but the reference type can only be null and cannot be instantiated. so it is useless to reference variables like this. for example, const int [] array = {1, 2, 3} is incorrect. however, const int [] array = null; is correct.
2. assign a value when declaring the value. You cannot change the value later. the variable value it modifies is determined during compilation. const int a = 4; yes. const int B; it is wrong if no value is assigned.
3. It is of the static type by default, so if it modifies the class field, it can only be directly referenced through the class, it cannot be referenced through the class instance. It cannot be used together with static to modify the variable.
Readonly
1. Only the fields of the class can be modified, and local variables cannot be modified.
2. assign values during declaration or assign values through constructors. values cannot be assigned elsewhere.
3. It is a little special when modifying the reference type. The referenced variable it modifies cannot reference other types when referencing a type, but the content in the referenced type can be modified.
For example, readonlyint [] array = {1, 2, 3}; we cannot create an array = new [] {4, 5, 6, 7} elsewhere. This will cause an error, however, we can change the value of array [0] = 100 in this way. This is correct.
Static
1. it can be used to modify class fields, functions, and classes. all functions and fields in the static class must be static. static fields and functions can only be referenced directly by class, but cannot be referenced by class instances.
2. static constructor. The declaration method of static constructor is similar to that of common constructor. Only static. static constructor can be initialized.
We do not need to call the static constructor by ourselves, which is called by the system by default. It is automatically called when a class is instantiated or when a static field is used. It will not be called once.
Static readonly
These two keywords can be used in combination. Most of the time, they have the same effect as const, but there are also some differences.
1. assign a value when const must be declared. static readonly can be assigned without a value first, and then a value in the static constructor.
2. const cannot modify the reference type. And static readonly can. For example, static readonly ClassName name = new ClassName (); is correct. At this time, it is wrong to use const to modify it.
3. Because the variable value modified by const is determined during compilation and the value of static readonly is determined during runtime, the same expression value may be different.
For example
Public class
{
Const int aa = bb + 10;
Const int bb = 10;
Staticreadonly int a = B + 10; // no value is assigned to B. The default value is 0. If B is placed before a, the result is 20.
Staticreadonly int B = 10;
Public voidPrintSom ()
{
Console. writeline (aa); // 20
Console. writeline (bb); // 10
Console. writeline (a); // 10
Console. writeline (B); // 10
}
}

 


From smart dummies

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.