First, the basic concept of variables:
Variable ability is used to store specific types of data, you can change the value of the data stored in the variable at any time. A variable has a name, type, and value. A variable must be declared before it is used, that is, the type and name of the specified variable.
1. Variable type:
Variable types are categorized according to their definition: one is a value type and the other is a reference type.
Second, the variable type;
1. Value type:
C # Simple Type collation: integer , float, and Boolean
①, integral type: int,sbyte ,byte, short, ushort, UINT, long, ulong , and Char
int: A signed three-bit integer with a range of values between -2,147,483,648~ 2,147,483,647 .
SByte: For signed 8 -bit integers, the value range is between 128~127 .
BYTE: is an unsigned , four-bit integer with a range of values between 0~255.
Short: A signed , Integer, range of values between -32,768~32,767 .
Type ushort: An unsigned integer with a range of values between 0~65,535 .
UINT: An unsigned four-bit integer with a value range between 0 and 4,294,967,295 .
Long: A signed integer with a value range of 9,223,372,036,854,775,808~ 9,223,372,036,854,775,807 .
ULONG: is a zero -bit unsigned integer with a range of values between 0 and 18,446,744,073,709,551,615 .
Create a console application that declares a variable of variable int type ls and initializes it to 927, a variable of type byte shj and initialized to 255, the output
1StatcivoidMain (string[] arge)2 {3 intls=927;//declaring a variable of type int ls4 byteSHJ =255;//declares a variable of type byte shj5Console.WriteLine ("Ls={0},shj={1}"+LS,SHJ);//Output6 console.readline ();7}
View Code
If an error is assigned to the variable shj of byte type 266, the byte value range 0~255
②, Boolean type:
BOOL: True and False values , used to store Boolean values of real and false.
1 bool x=927;
View Code
Boolean data types have a Boolean value of true and false two. A Boolean variable can be assigned a value of true or false , or it can be assigned to an expression whose value is equal to one of the two:
BOOL Btest = (> 90);
③, floating-point
Two data types are treated as floating-point: float and double. They differ in the range and accuracy of the values:
float: The value range is between 1.5x10^-45~ 3.4x10^38 , with a precision of 7 digits.
1 float themysum=9.27f; // use F to force designation to float type 2 float themysums=1.12F; // use F to force designation to float type
View Code
Double: the range of values is between 5.0x10^-324 ~ 1.7x10^308 and the precision is 15~16 digits.
1 Double mydou=927d; // use D to cast to double 2 double mudou=112d; // use D to cast to double
2. Reference type:
Reference types are the primary object type data for building C # applications, with pre-defined object types creating object instances with new and storing them on the stack.
All referred to as "classes" are reference types, mainly including classes, interfaces, arrays, and delegates.
Create a console application in which you create a class C, establish a field value in the class, and initialize it to 0. The reference-type variable of the class is then created from another location through new and the final output.
1 class Program2 {3 classC//create a Class C4 {5 Public intValue =0;//declares a variable of the public int type value6 }7 Static voidMain (string[] args)8 {9 intV1 =0;//declares a variable of type int v1 and initializes it to 0Ten intV2 =1;//declares a variable of type int v2, and assigns V1 a value of v2 OneV2 =927;//re-assigns the variable V2 to 927 AC R1 =NewC ();//To create a reference object using the New keyword -C r2 = R1;//make R1=R2 -R2. Value = the;//set the value of the R2 theConsole.WriteLine ("Values; {0},{1}", v1, v2);//setting variables for Output v1 and v2 -Console.WriteLine ("refs; {0},{1}", R1. Value, R2. Value);//value of the output reference type Object - console.readline (); - } +}
C # Fundamentals-Variables and Constants (1)