. C #-struct type, reference type

Source: Internet
Author: User

struct type: value type, and class is reference type

Structure definition:

    • Structs are defined in much the same way as classes, but structs are value types, and classes are reference types;

    • object is a reference type, when assigning an object to a variable, it assigns a pointer to the object that the pointer points to, changes one of the values, and the value changes;

Using system;using system.collections.generic;using system.linq;using system.text;using  system.threading.tasks;namespace consoleapplication1{    class program     {        class MyClass         {            public  int val;        }         struct MyStruct        {             public int val;         }        static void main (String [] args)         {             myclass objeCta = new myclass ();             myclass objectb = objecta;             objectA.val = 100;             Objectb.val = 200;            mystruct  structa = new mystruct ();             MyStruct StructB = StructA;             StructA.val = 100;             StructB.val = 200;             console.writeline ("object a = {0}", Objecta.val);             consoLe. WriteLine ("object b = {0}", Objectb.val);             console.writeline ("struct a = {0}", Structa.val);             console.writeline ("struct b = {0}",  Structb.val);             console.readline ();         }    }}

Results:

Object A =200
Object B =200
Struct A =100
Struct B =200


This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1651625

. C #-struct type, reference type

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.