C # Learning Series-differences between classes and structures,

Source: Internet
Author: User

C # Learning Series-differences between classes and structures,

Refer:Http://www.microsoftvirtualacademy.com/Content/ViewContent.aspx? Et = 9851 & m = 9830 & ct = 31038

If you have any questions, please correct them.

Class:The reference type is stored in the heap, And the stack stores the reference address. In method transmission, only the reference of the transmission address is used. Modifying the object to which it points will affect the value of the original object, low memory consumption during transmission.

Structure:Value type, which is stored in the stack. A copy of the entire object is transmitted during transmission. Modifying the value pointing to the object does not affect the original object, which consumes a large amount of memory during transmission.

Post Code below

Class Program {static void Main (string [] args) {/* declares the class Object and assigns a value of 10 */TestClass TC1 = new TestClass (); TC1.x = 10; TC1.y = "10"; Console. writeLine ("/* declare Class Object TC1 and assign 10 */"); Console. writeLine ("TC1 x = {0} y = {0}", TC1.x, TC1.y); TC1.x = 20; TC1.y = "20"; Console. writeLine ("/* modify the Class Object TC1 value to 20 */"); Console. writeLine ("TC1 x = {0} y = {0}", TC1.x, TC1.y);/* execution class passed and modified the passed value */Console. writeLine ("/* Create A Class Object TC2 pointing to TC1 and change the value to 10 */"); TestClass TC2 = TC1; TC2.x = 10; TC2.y = "10"; Console. writeLine ("/* during class transmission, the reference address stored in the stack is transmitted, which consumes a small amount of memory and does not transmit the value stored in the heap. Therefore, the original object is affected */"); console. writeLine ("TC1 x = {0} y = {0}", TC1.x, TC1.y); Console. writeLine ("TC2 x = {0} y = {0}", TC2.x, TC2.y);/* declare structure object */Console. writeLine ("/* declare the structure object TS1 and assign a value of 10 */"); TestStruct TS1 = new TestStruct (); TS1.x = 10; TS1.y = "10"; Console. writeLine ("TS1 x = {0} y = {0}", TS1.x, TS1.y); Console. writeLine ("/* modify the TS1 value of the structure object to 20 */"); TS1.x = 20; TS1.y = "20"; Console. writeLine ("TS1 x = {0} y = {0}", TS1.x, TS1.y);/* execute Structure Transfer and modify the passed value */Console. writeLine ("/* create a structure object TS2 pointing to TS1, and change the value to 10 */"); TestStruct TS2 = TS1; TS2.x = 10; TS2.y = "10"; Console. writeLine ("/* the entire object will be transmitted during structure transmission, which consumes a large amount of memory. Therefore, modification is not affected to the original object */"); Console. writeLine ("TS1 x = {0} y = {0}", TS1.x, TS1.y); Console. writeLine ("TS2 x = {0} y = {0}", TS2.x, TS2.y); Console. readLine () ;}} public class TestClass {public int x; public string y;} public struct TestStruct {public int x; public string y ;}

 


C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

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.