1, passing by value
Values are passed by value, the substance of the reference type by value is the pass-through value, when the argument is a value type, the "value" is the instance itself, so the instance copy is passed, the original instance is not affected; When the argument is a reference type, "value" is the object reference, so the reference address copy is passed to the original object.
String is a reference type, and string is passed by value in the same way that a value type is passed by value, and string is special here.
When an invocation method takes a parameter pass, the method creates a variable in the stack based on the parameter type, and assigns the value of the parameter to the variable. Therefore, the value type passes an instance of the string type unchanged, and the reference type passes the address change. However, if you pass by reference, it is the delivery address, and the value of the instance changes.
2, passing by reference
Ref and out passed by reference, regardless of value type or reference type, pass by reference must be decorated with ref or out keyword, ref requires that arguments before passing must first show initialization, and out is not required. That is, a parameter that uses ref must be an actual object, not NULL, and an out parameter can accept an object that points to null, and then the object must be manifested within the calling method.
Value types are not boxed for value types when they are passed by reference.
Passed by reference, not the value of the parameter itself, but the address of the parameter. If the argument is a value type, the address of the value type is passed, and if the argument is a reference type, the address of the object reference is passed, and the reference type passes the result by reference and by value by reference.
int i = 100;//ref need to display initialize int j;//out do not need to display initialization fun (ref i,out j); void fun (ref int i,out int J) { J = 100;//out must complete the object's entity }
As follows:
private void Awake () { Sendertest st = getcomponent<sendertest> ();//The object of the instantiated class st.i =; int i = +; string s = "n"; ChangeValue (I,s, ST);//value type, reference type, pass print by value ("value" +i+s+st.i),//value, changevalue (ref I, ref s, ref ST);//value type, reference type, pass print by reference ("ref" +i + S + st.i);//ref (+), changevalue (int i, string s, Sendert EST st) { i = $; s = "$"; ST.I = $; } void ChangeValue (ref int I, ref string S, ref sendertest St) { i = n; s = "$"; ST.I = $; }
Related articles:
Writing PHP Extension in C + +
C # Tutorial C # data type