c # special treatment - constants, variables and assignment

constant

Constant is well understood, and variable relative amount of change is not. For example, 1 is a constant, the constant is 3.6, 'a' is a constant, "aaaaa" is constant, but different types. These are on the surface at a glance constant, there is a surface does not come out of constants, symbolic constants, also known as defined constants.

For example, I use a program to be repeated a few particularly long, but I was afraid to write more than a mistake, then you can use symbolic constants, define an identifier, you use it to replace the. The wording is "const data type identifier (name)":

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace hello
{
    class Program
    {
        static void Main(string[] args)
        {

            const double pi= 3.14; //使用const定义符号常量
            Console.WriteLine(pi);
            Console.ReadKey();
        }
    }
}

Operating results is 3.14, although we write code is output pi, but the execution is output Shique 3.14, because we use a symbolic constant pi instead of 3.14.

 variable

As the name suggests, it is a variable amount will become. Define the variable method is the "data type identifier (variable name)." Such as:

int num;

This defines good an integer variable num.

Assignment

Variable is the equivalent of a container, which is a value we need, we can find this value by the variable name. So the variables to be assigned before you can use, such as:

a = 1;

This gives the above-defined variable assignment num 1 a.

Must pay attention to, "=" Here is the assignment of meaning, is not equal to the number, and mathematics do not confuse!

You must first define variables before you can use the assignment, but normally we would give it a value directly at the point of definition, such as:

int num = 1;

We look at the results achieved:

 

Guess you like

Origin www.cnblogs.com/hmswt/p/11308539.html