HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
A variable is just a name for the operation of the program memory area. In C #, each variable has a specific type, type determines the memory size and layout variables. The range of values can be stored in memory, you can perform a series of operations on variables.
We have discussed a variety of data types. The basic value types in C # offer can be divided into the following categories:
类型 | 举例 |
---|---|
整数类型 | sbyte、byte、short、ushort、int、uint、long、ulong 和 char |
浮点型 | float 和 double |
十进制类型 | decimal |
布尔类型 | true 或 false 值,指定的值 |
空类型 | 可为空值的数据类型 |
C # allows you to define variables other value types, such asenum, but also allows you to define a reference type variables, such as class.These will be discussed in later chapters. In this section, we only study the basic variable types.
C # syntax defined variables:
<Data_type> <variable_list>;
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 effective variables are defined as follows:
int i, j, k; char c, ch; float f, salary; double d;
You can be initialized when the variable definition:
int i = 100;
Variable equal sign followed by a constant expression initialized (assigned). The general form of initialization is:
variable_name = value;
Variables can be initialized (specify an initial value) at the time of declaration. Initialized by an equal sign followed by a constant expression, as shown below:
<Data_type> <variable_name> = value;
Some examples:
int d = 3, f = 5; / * initialize d and f * /. byte z = 22; / * initialize z * /. double pi = 3.14159; / * declaration approximation of pi * / char x = 'x'; / * variable x value of 'x' * /
Properly initialized variable is a good programming practice, otherwise the program will sometimes produce unexpected results.
Consider the following examples, the use of various types of variables:
namespace VariableDefinition { class Program { static void Main (string [] args) { short a; int b; double c; / * Actual initialization * / a = 10; b = 20; c = a + b; Console.WriteLine ( "a = {0}, b = {1}, c = {2}", a, b, c); Console.ReadLine (); } } }
When the above code is compiled and executed, it produces the following results:
a = 10, b = 20, c = 30
E.g:
int num; num = Convert.ToInt32 (Console.ReadLine ());
FunctionConvert.ToInt32 () the data entered by the user is converted to int data type because Console.ReadLine ()only accepts data string format.
C # in two expressions:
lvalue: lvalue expression can occur in the assignment of the left or right.
rvalue: rvalue expression can appear on the right side of an assignment statement can not appear on the left side of an assignment statement.
Variable is lvalue, it may appear to the left of an assignment statement. Rvalue is the value, and therefore can not be assigned, it can not appear on the left side of an assignment statement. The following is a valid statement:
int g = 20;
The following is a valid statement will produce a compilation error:
10 = 20;