How to Use the C # keyword const, readonly, static,

Source: Internet
Author: User

How to Use the C # keyword const, readonly, static,

If a value does not change much, we often use const and readonly. What are the differences between the two? Sometimes, we add the keyword static before readonly. What does this mean?

 

Const

● The const is static by default and can be accessed through "class name. Field name.
● The const variable can only be assigned a value when declared, and cannot be assigned a value for the const type variable in the const function.
● Once the Assembly is compiled, the const variable is written into the Assembly's IL code. If you want to modify the const variable value, you must re-generate the Assembly after modifying the value.
● Const is the variable of the compilation period.

    public class Test
    {
        public const int defaultValue = 10;
// The following error is reported: the const variable cannot be assigned a value in the const variable.
        public Test()
        {
            defaultValue = 1000;
        }
    }

Above,
You can use Test. defaultValue to obtain the value of the defaultValue variable.
An error is returned when defaultValue is assigned in the Test structure. The initial value can only be assigned when defaultValue is declared.

 

Readonly

● Readonly is an instance variable by default and can only be accessed through "object instance. Field name.
● The readonly variable can be assigned a value during declaration or within the constructor.
● To change the value of the readonly variable, you only need to modify it in the declared variable or constructor. You do not need to regenerate the assembly.
● Readonly is the runtime variable.

    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            Console.WriteLine(test.rdValue);
            Console.ReadKey();
        }
    }
    public class Test
    {
        public readonly int rdValue;
        public Test()
        {
            this.rdValue = 100;
        }
// Here, an error is reported: The readonly variable can only be assigned a value in the declared variable or constructor.
        public int RDVaue
        {
            get { return rdValue; }
            set { rdValue = value; }
        }
    }


Above,
Get the readonly variable value through the Test class instance, test. rdValue.
If you assign a value to the readonly variable RDValue in the rdValue attribute, an error is returned.

 

If the static keyword is added before readonly:
● The readonly variable can only be assigned a value when the variable is declared.
● The readonly variable can be accessed through "class name. Field name ".
● The readonly variable is changed to the compile-time variable.

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Test.rdValue);
            Console.ReadKey();
        }
    }
    public class Test
    {
        public static readonly int rdValue=100;
    }

 

Above,
Values can only be assigned when the static readonly variable rdValue is declared.
Use Test. rdValue to access the static readonly variable.

 

Conclusion: const is a static variable and can only be assigned a value when the variable is declared. readonly is a runtime variable and can be assigned a value when declared or within the constructor. When the keyword static is added before readonly and becomes static readonly, the static readonly variable becomes static and compile-time variable.


How to use C ??

Switch with if:

# Include <stdio. h>

Void main ()
{
Int I, j;
Printf ("Enter your exam score :");
Scanf ("% d", & I );
If (I> = 0 & I <= 59)
J = 1;
If (I> = 60 & I <69)
J = 2;
If (I> = 70 & I <= 79)
J = 3;
If (I> = 80 & I <= 89)
J = 4;
If (I> = 90 & I <= 99)
J = 5;
Switch (j)
{
Case 1:
{
Printf ("your score is: E \ n ");
Break;
}
Case 2:
{
Printf ("your score is: D \ n ");
Break;
}
Case 3:
{
Printf ("your score is C \ n ");
Break;
}
Case 4:
{
Printf ("your score is B \ n ");
Break;
}
Case 5:
{
Printf ("your score is A \ n ");
Break;
}
Default:
Printf ("your score is not within the exam score range! \ N ");

}
}

If multi-branch structure statements are written separately:
# Include <stdio. h>

Void main ()

{
Int I;
Printf ("Enter the exam score :");
Scanf ("% d", & I );
If (I <= 0 | I> = 100)
Printf ("beyond the Score Input range! \ N ");
If (I <= 99 & I> = 90)
{
Printf ("score :");
Printf ("A \ n ");
}

If (I <= 89 & I> = 80)
{
Printf ("score :");
Printf ("B \ n ");
}
If (I <= 79 & I> = 70)
{
Printf ("score :");
Printf ("C \ n ");
}
If (I <= 69 & I> = 60)
{
Printf ("score :");
Printf ("D \ n ");
}
If (I <= 59 & I> = 0)
{
Printf ("score :");
Printf ("E \ n ");
}
}

Separate switch statements:

# Include <stdio. h>

Void main ()
{
Int I, j;
Printf ("Enter your exam score :");
Scanf ("% d", & I );
J = I/10;
Switch (j)
{
Case 9:
{
Printf ("your exam score is: A \ n" ...... the remaining full text>

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

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.