Understanding C # Reference types

Source: Internet
Author: User

Fall out of the hole, because the understanding of the reference type is not deep enough, resulting in a bug in the project.

Let's start with the C # Basics: The reference type assignment assigns the pointer to the object, and the value type assignment is the copy copy, which is one of the differences between the reference type and the value type.

On the code:

1  class Program2     {3         Static voidMain (string[] args)4         {5             varobj =Myclassa.propertya;6Console.WriteLine (string. Join (",", MyClassA.PropertyA.Foo));7Obj. Foo.removeall (M = = m = ="e");8Console.WriteLine (string. Join (",", MyClassA.PropertyA.Foo));9 TenConsole.WriteLine (string. Join (",", MyClassA.PropertyA.Bar)); OneObj. Bar = obj. Bar.where (m = m >2). ToList (); AConsole.WriteLine (string. Join (",", MyClassA.PropertyA.Bar)); -  -Obj. Foo =Newlist<string> {"x","y","Z"}; theConsole.WriteLine (string. Join (",", MyClassA.PropertyA.Foo)); -  - Console.read (); -         } +     } -     classMyclassa +     { A         StaticMyclassa () at         { -Propertya=NewMYCLASSB (); -Propertya.foo =Newlist<string> {"a","b","C","D","e"}; -Propertya.bar =Newlist<int> {1,2,3,4,5}; -         } -          Public StaticMYCLASSB Propertya {Get;Private Set; } in     } -  to     classMYCLASSB +     { -          Publiclist<string> Foo {Get;Set; } the          Publiclist<int> Bar {Get;Set; } *}

It is not easy to understand is: I am operating in the internal member of obj, why even Myclassa.propertya member values have been changed?

Because, when Myclassa.propertya is assigned to obj, obj points to Myclassa (reference type), so as long as the operation on the obj member is the internal member of the Operation Mycalssa.

Understanding C # Reference types

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.