First, a section of code.
usingUnityengine;usingSystem.Collections; Public classTypepassing:monobehaviour { PublicDog dog =NewDog (1,"Xiaobai") ; //call the default constructor to a default value for all fields in the struct, which is null for dog PublicCat cat =NewCat ();//I don't seem to be able to assign the dog attribute directly here . inti =0; voidStart () {Cat=NewCat (Dog,1,"Xiaohua"); Parameterpass (dog); Debug.Log (Dog.number+ Dog.name);//1xiaobaiParameterpass ( outdog); Debug.Log (Dog.number+ Dog.name);//8yuziParameterpass (i); Debug.Log ("i="+i);//i=0Parameterpass (refi); Debug.Log ("i="+i);//i=2Debug.Log (cat.name + cat.number + cat.dog.name);//Xiaohua1xiaobaiParameterpass (CAT); Debug.Log (Cat.name+ Cat.number + cat.dog.name);//Xiaohua1xiaobai } //a reference pass without ref, which affects the original object only if the field of the object is changed//speculation is that a new variable points to the original address, and copies a copy of the value type directly. voidParameterpass (dog dog) {Dog=NewDog (6,"Xiaolong"); } //passing with ref is a complete reference pass, and whatever you do with the parameter will affect the original object /*void Parameterpass (ref dog dog) {dog = new dog (7, "Feizi"); }*/ //out is similar to ref, do not make any copy, pass the reference directly//out and ref cannot be overloaded, and methods without out, ref can be overloaded voidParameterpass ( outdog dog) {Dog=NewDog (8,"Yuzi"); } voidParameterpass (inti) {i=1; } voidParameterpass (ref inti) {i=2; } voidParameterpass (cat cat) {Cat.name="Simao"; Cat.dog=NewDog (8,"Yuzi"); Cat.dog.name="Feizhu"; }} Public classdog{ Public intNumber ; Public stringname; PublicDog (intNumberstringname) { This. Number =Number ; This. Name =name; }} Public structcat{ PublicDog Dog; Public intNumber ; Public stringname; PublicCat (Dog Dog,intNumberstringname) { This. Dog =Dog; This. Number =Number ; This. Name =name; }}
C # 's value types include structs and enums, and our commonly used int, byte, and so on are a special type of value, declaring an int type, actually declaring a system.int32 struct type, and all value types implicitly inherit from System.ValueType. The reference types for C # include classes, interfaces, and delegates.
A parameter that passes a value type is simply a copy of the value that is passed to the method, passing the argument of the reference type, possibly passing a new variable to the object to use for the method. Assigning the variable does not affect the original object, but changing the field property of the variable affects the original object, because the variable and the method external variable point to the same object within the heap.
When using ref and out, the original variable is passed. Making any changes to the parameters is equivalent to altering the original variable directly.
So when a struct type contains a reference type, does the value pass with a parameter without ref? The non-referenced part of the structure is definitely not affected. For the reference part, through the experiment, also does not affect.
Analysis of C # value passing and reference value passing