1. 3 type
C # supports two basic types: value types and reference types ). Values include simple types
(Char, int, and float), enumeration (enum), and structure (struct ). References include class, interface ),
Delegate and array ). The difference between a value and a reference is that a value directly stores its data content, and the reference storage object
. Is it confusing ?! For example. You bought a villa somewhere (great ). But I have never been there. I only know the address. How?
? You can take a taxi and the driver will know how to take the taxi. The address in your hand is like the object name.
It is written in a program, as if to give the address to the driver. The driver is your compiler and it knows where to go. Your luxurious house is like that
Ngws sdk (82mb Oh, luxurious enough! Ah, I am ). There is something you want in the house, for example, you want to write
I dont like Hello world, as in the above example, "WriteLine" is used ". So you will give "WriteLine"
For example, "Console. WriteLine ". Understand ?! I am so tired. Zzz... I don't know if you think of it,
The difference between values and references can lead to an important feature. The value variables correspond to the data stored in the variables one by one and are unique. But the reference is not
However. Different variables in the reference can reference instances of the same object. When one of the variables changes the value of the instance
The variable is also affected (of course, the variable itself is not changed, that is, the address is not changed ). Look, variables only indicate the location of the stored object (location
Instead of the object itself. It's like your beautiful house has been burned, but your address hasn't changed, but the house corresponding to the address is gone.
Maybe someone else has this address. He burned your house! Okay. Here is an example :*/
1: using System;
2: class CValue
3 :{
4: public int Value = 0;
5 :}
6: class Test
7 :{
8: static void Main (){
9: int val1 = 0;
10: int val2 = val1;
11: val2 = 123;
12: CValue ref1 = new CValue ();
13: CValue ref2 = ref1;
14: ref2.Value = 123;
15: Console. WriteLine ("Values: {0}, {1}", val1, val2 );
16: Console. WriteLine ("Refs: {0}, {1}", ref1.Value, ref2.Value );
17 :}
18 :}
/* The output result is as follows:
Values: 0,123
Refs: 123,123
Aha, it should be clear. The val1 and val2 variables do not affect each other. They have their own storage space. Ref2 Replication
So they reference instances of the same object. When one of them is changed, it will affect the other
Value.