Home > Backend Development > C#.Net Tutorial > [c# tutorial] C# variables

[c# tutorial] C# variables

黄舟
Release: 2016-12-26 13:57:27
Original
1167 people have browsed it

C# Variables

A variable is just the name of a storage area for program operations. In C#, each variable has a specific type, and the type determines the memory size and layout of the variable. Values ​​within the range can be stored in memory, and a range of operations can be performed on the variables.

We have discussed various data types. The basic value types provided in C# can be roughly divided into the following categories:

types

examples

integer types sbyte, byte, short, ushort, int, uint, long, ulong and char

floating point type float and double

decimal type decimal

boolean type true or false value , the specified value

null type data type that can be null

C# allows the definition of variables of other value types, such as enum, and also allows the definition of reference type variables, such as class. We will discuss these in later chapters. In this chapter, we only study basic variable types.

Variable definition in C#

Syntax of variable definition in C#:

<data_type> <variable_list>;
Copy after login

Here, data_type must be a valid C# data type, which can be char, int, float, double or other user-defined data types. variable_list can consist of one or more identifier names separated by commas.

Some valid variable definitions are as follows:

int i, j, k;
char c, ch;
float f, salary;
double d;
Copy after login

You can initialize at variable definition time:

int i = 100;
Copy after login

Variable Initialization in C#

Variables Initialization (assignment) is performed by following the equal sign with a constant expression. The general form of initialization is:

variable_name = value;
Copy after login

Variables can be initialized (specify an initial value) when declared. Initialization consists of an equal sign followed by a constant expression, as shown below:

<data_type> <variable_name> = value;
Copy after login

Some examples:

int d = 3, f = 5;    /* 初始化 d 和 f. */
byte z = 22;         /* 初始化 z. */
double pi = 3.14159; /* 声明 pi 的近似值 */
char x = &#39;x&#39;;        /* 变量 x 的值为 &#39;x&#39; */
Copy after login

It is a good programming practice to initialize variables correctly, otherwise sometimes the program will behave unexpectedly Unexpected results.

Look at the following example, using various types of variables:

namespace VariableDefinition
{
    class Program
    {
        static void Main(string[] args)
        {
            short a;
            int b ;
            double c;

            /* 实际初始化 */
            a = 10;
            b = 20;
            c = a + b;
            Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);
            Console.ReadLine();
        }
    }
}
Copy after login

When the above code is compiled and executed, it produces the following results:

a = 10, b = 20, c = 30
Copy after login

Accepting values ​​from the user

The Console class in the System namespace provides a function ReadLine() for receiving input from the user and storing it in a variable.


For example:

int num;
num = Convert.ToInt32(Console.ReadLine());
Copy after login

The function Convert.ToInt32() converts the data entered by the user into the int data type, because Console.ReadLine() only accepts characters Data in string format. Lvalues ​​and Rvalues ​​in

C# Two expressions in

C#:

lvalue: An lvalue expression can appear on the left or right side of an assignment statement.

rvalue: The rvalue expression can appear on the right side of the assignment statement, but cannot appear on the left side of the assignment statement.

variables are lvalues, so they can appear on the left side of assignment statements. Numeric values ​​are rvalues ​​and therefore cannot be assigned and cannot appear on the left side of an assignment statement. The following is a valid statement:

int g = 20;
Copy after login

The following is an invalid statement, which will generate a compile-time error:

10 = 20;
Copy after login

The above is the content of C# variables in [C# Tutorial], for more related information, please Follow the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template