C # (3) Type

Source: Internet
Author: User

1. 3 type

C # supports two basic types: value types and reference types ). Values include char, int, float, enum, and struct ). References include classes, interfaces, delegate, and array arrays ). 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. What should I do? You can take a taxi and the driver will know how to take the taxi. The address in your hand is like the name of the object. You write it in a program and it is like giving the address to the driver. The driver is your compiler and it knows where to go. Your luxurious house is like the ngws sdk (82mb Oh, luxurious enough! Ah, I am ). There is something you want in the house. For example, if you want to write a sentence (I dont like Hello world), it is like the above example, you need to use "WriteLine ". Therefore, you will give the "WriteLine" address, 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. Different variables in the reference can reference instances of the same object. When one of the variables changes the Instance value, other variables that reference this instance will also be affected (of course, the variable itself has not changed, that is, the address has not changed ). Look, variables only indicate the location (address) of the stored object, not 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 copies ref1, so they reference instances of the same object. When you change one of them, it will affect the other value.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.