Value types and reference types
Common types of data
| Plastic |
Int |
| Floating point Type |
Foalt |
| Double-precision floating-point type |
Double |
| String |
String |
| Boolean |
bool |
| Enumeration |
Enum |
Value type
Value types inherit from the System.ValueType class, and each value-type object has a separate memory region to hold its own value, and the area of memory where the value type data resides is called a stack (stack). As long as you modify it in your code, the value is saved within its memory area.
Reference type
Reference types inherit from the System.Object class, and in C # Reference types mainly include arrays, classes, interfaces, and so on.
Fine-score types and reference types
Value type:
Basic data type:
| Plastic |
Int |
| Long Plastic |
Long |
| Floating point Type |
Foalt |
| Double-precision floating-point type |
Double |
| Character type |
Char |
| Boolean type |
bool |
Enum type: enum
struct type: struct
Reference type:
Class:
| Base class |
System.Object |
| String |
String |
| Custom Classes |
Class |
Interface: interface
Array: Int "", string ""
Structure
Structure definition
access modifier sturct struct name {// struct }
Features of the structure:
- Fields within a structure cannot be assigned an initial value when defined
- Use of structures
- Structures can be defined without a new direct definition of the structure's objects
- The structure must be assigned an initial value after declaring the object of the structure
- Demo
Public struct Student { public int. ID/; ID public int ages // Age
Public voidSayhi () {Console.WriteLine ("School Number:"+id+"Age:"+Age )} } //structure Definition
Public Static voidMain (string[] args) {student Stu; Stu.id=1234; Stu.age= -; Stu.sayhi ();} //struct Call
Value Mode parameter passing
When a value-mode parameter is passed, the parameter is a value type, and the value is unchanged after the call, which is the reference-type-value variable
Reference method parameter passing
When a reference method parameter is passed, the value is variable regardless of the parameter value type or the reference type call
Demo: Value mode pass value type parameter
Public void addage (int age ) { age + +; } Static void Main (string[] args) { new Stu (); int 3 ; Console.WriteLine (num); Sb.addage (num); Console.WriteLine (num); }
Results:
Value method pass Reference type parameter
Public classStu { Public intAge ; Public voidaddage (Stu Student) {Student.age++; } } Public Static voidMain (string[] args) {stu SB=NewStu (); Sb.age=3; Console.WriteLine (Sb.age); Sb.addage (SB); Console.WriteLine (Sb.age); }
Results:
To pass a value type parameter by means of a reference
Public void ref int Age ) { age + +; } Static void Main (string[] args) { new Stu (); int 3 ; Console.WriteLine (num); Sb.addage (ref num); Console.WriteLine (num); }
Results:
To pass a reference type parameter in a reference way
Public classStu { Public intAge ; Public voidAddage (refStu Student) {Student.age++; } } Public Static voidMain (string[] args) {stu SB=NewStu (); Sb.age=3; Console.WriteLine (Sb.age); Sb.addage (refSB); Console.WriteLine (Sb.age); }Results:
Drill down into C # data types