Understanding the difference between passing a struct to a method and passing a class reference to a method (C # Programming Guide)

Source: Internet
Author: User

The following example shows how to use a struct-to-method to pass to a method differently from a class instance. in this example, two parameters (struct and class instances) value and two methods by changing the value of one field of the parameter. However, the results of these two methods are different because of the passing of the structure, and when you pass through it, you can pass an instance of the class.

Because the structure is a value type, when you use a struct value on a method, the method is subjected to a copy of the structure parameter. The method cannot be accessed in the original structure by calling the method and cannot change it in any way. This method can only change the copy.

An instance of a class is a reference type, not a value type.When the reference type of the other method is referenced by value, the method is copied to the class instance.That is, the method receives a copy of the instance, not the address of the replicated instance.A class instance that invokes a method has an address that invokes a copy of the address of the parameter of the method, so that two addresses refer to the same object.because the parameter contains a copy of the address, the calling method cannot change the address of the class instance in the calling method. However, the calling method can use that address to access the original address and the class member referenced by the replica. if the calling method will be a class member, the instance of the original class that invokes the method also changes.

The output of the following example shows the difference., because the method uses the address in the argument to find an instance of the class, the specified field of the class instance is calledthe value of the Willichange field is passed to method Classtaker. Call the Willichange field without changing the structure in the calling method to method Structtaker, because the value of the parameter is a copy of the structure instead of copying its address. structtaker Changes the copy so that the copy is lost when the call to Structtaker is complete.

C#
Class theclass{public    string Willichange;} struct thestruct{public    string Willichange;} Class testclassandstruct{    static void Classtaker (Theclass c)    {        C.willichange = "Changed";    }    static void Structtaker (Thestruct s)    {        S.willichange = "Changed";    }    static void Main ()    {        Theclass TestClass = new Theclass ();        Thestruct teststruct = new Thestruct ();        Testclass.willichange = "Not Changed";        Teststruct.willichange = "Not Changed";        Classtaker (TestClass);        Structtaker (teststruct);        Console.WriteLine ("Class field = {0}", Testclass.willichange);        Console.WriteLine ("Struct field = {0}", Teststruct.willichange);        Keep the console window open in debug mode.        Console.WriteLine ("Press any key to exit.");        Console.readkey ();    }} /* Output:    Class field = Changed    Struct field = Not Changed

Understanding the difference between passing a struct to a method and passing a class reference to a method (C # Programming Guide)

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.