In-depth C # data types,
C # Common Data Types
Common Data Types java C # example
Int age
Float results
Double circumference Rate
String string String home address
Whether boolean bool is a minority
Enum color
Value Type
The value type is derived from the System. ValueType family. Each value type object has an independent memory region used to save its own value. The memory region of the value type data is called a Stack)
Value types mainly include basic data types (such as int, float, and double)
Case:
Int heightzhang= 170;
Int heightli = heigtzhang;
Console. writeLine ("Zhang Hao's score last year:" + heightzhang + "Li Ming's score:" + heighli );
Heightli= 180;
Console. writeLine ("Zhang Hao's score last year:" + heightzhang + "Li Ming's score:" + heighli );
Reference Type
The reference type is derived from the System. Object family. in C #, the reference types mainly include arrays, classes, and interfaces.
Case:
Int [] infozhang = new int [] {179,60 };
Int [] infoli = infozhang;
Console. write ("last year, Zhang Hao's height was:" + infozhang [0] + "Weight:" + infozhang [1] + "Li Ming's height is: "+ infoli [0] +" Weight: "+ infoli [1]);
Infoli [0] = 180;
Infoli [1] = 70;
Console. write ("last year, Zhang Hao's height was:" + infozhang [0] + "Weight:" + infozhang [1] + "Li Ming's height is: "+ infoli [0] +" Weight: "+ infoli [1]);
Subitem type and reference type
Data Type Classification
Category Description
Value Type basic data type Integer: int
Long Integer: long
Float
Double Type: double
Character Type: char
Boolean: bool
Enumeration type enumeration: enum
Structure Type Structure: struct
Reference Type Base Class: System. Object
String: string
Custom class: class
Interface: interface
Array: int [], string []
Structure
Public int id;
Public int age;
Public int Id {
Get {return id ;}
Set {id = value ;}
}
Public int Age {
Get {return age ;}
Set {age = value ;}
}
Structure Definition
Access modifier struct structure name {
// Result body
}
The structure definition has the following features:
1. The structure can contain fields or methods.
2. During definition, fields in the structure cannot be assigned a value.
Public struct Student {
Public int id;
Public int age;
}
Structure usage
The structure is similar to the class. When using the structure, pay attention to the following aspects:
1. You can directly define the structure object without using new'
2. After declaring the object of the structure, the initial value must be assigned to the structure member.
Public struct Student {
Public int id;
Public int age;
Public void Show (){
Console. writeLine ("ID: {0} \ n age: {1}", id, age );
}
}
Static void Main (string [] args ){
Student stu;
Stu. id = 1001;
Stu. age = 20;
Stu. show ();
}
Structure usage experience
The structure is a value type, and the declared Structure Variable stores a new copy of the structure, that is, the system needs to open up a new storage space. Therefore, the more the structure is used, the more space it consumes.
Packing and unpacking
We can say that data types can be divided into value types and reference types by storage method, and they can still be converted to each other. The process of converting a value type to a reference type is called packing, and vice versa.
Static void Main (string [] args ){
Int I = 123;
Object o = I;
I = 456;
Console. writeLine ("value type value: {0}", I );
Console. writeLine ("reference type value: {0}", o );
}
Unpack
Int I = 123;
Object o = I;
Int j = (int) o;
Passing different types of parameters
1. Use value transfer. Changes to parameter values in the method cannot be retained after the call.
2. Therefore, the ref method is passed and changes to the parameter value can be retained.
Value mode parameter transfer
When a value type parameter is passed, the parameter can be of the reference type or value type.
Use the reference type as a parameter
Class Voter {
Public void Vote (SE se ){
Se. Popularity ++;
}
}
SE zhang = new SE ();
Zhang. Age = 25;
Zhang. Name = "zhang Jing ";
Zhang. Gender = Gender. female;
Zhang. Popularity = 10;
MessageBox. Show (zhang. SayHi ());
Voter voter = new Voter ();
Voter. Vote (zhang );
MessageBox. Show (zhang. SayHi ());
Parameter transfer in Reference Mode
1. Use the reference type as the parameter
Private void Vote (ref SE se ){
Se. popularity ++;
}
2. Use the value type as a parameter
Private void Vote (ref StructSE se ){
Se. popularity ++;
There is no difference between the two parameters passed in the ref method, and the changes in the method will be saved.
}
Unpack