Difference between values and referenced parameters:
In C#, parameters can be passed either by values or by reference. Passing parameters through references in the calling environment allows function members (methods, properties, indexers, operators, and constructors) to change the value of the parameter and keep the change. To pass parameters by reference, use the ref or out keywords.
The following example illustrates the difference between a value and a referenced parameter:
class Program { static void Main(string[] args) { int arg; // Passing by value. // The value of arg in Main is not changed. arg = 4; squareVal(arg); (arg); // Output: 4 // Passing by reference. // The value of arg in Main is changed. arg = 4; squareRef(ref arg); (arg); // Output: 16 } static void squareVal(int valParameter) { valParameter *= valParameter; } // Passing by reference static void squareRef(ref int refParameter) { refParameter *= refParameter; } }
The difference between passing a structure to a method and passing a class reference to a method
The following example demonstrates how to use a structure to a method differently than passing it to a method through a class instance. In this example, two parameters (struct and class instance) will have the value and the two methods by changing the value of a field of the parameter. However, the results of these two methods are different because the passing of the structure when you pass through what is different, then you can pass through the instance of the class.
Since structures are value types, then when you use structure values for methods, the method is subject to a copy of the structure parameters. Methods cannot access the method in the original structure and cannot change it in any way. This method can change only the copy.
An instance of a class is a reference type, not a value type. When the reference type of a method is passed through a value, the method is copied to the class instance. That is, the method is subject to an instance, rather than a copy of the address of the instance. The class instance in which the method is called has an address, a copy of the address of the parameter in which the method is called, so whether the two addresses refer to the same object. Since this parameter contains a copy of the address, the calling method cannot change the address of the class instance when the calling method is called. However, the calling method can use that address to access the original address and the class members referenced by the copy. If a method is called to a class member, the instance of the original class in which the method is called will also change.
The output of the example below shows the difference. , because the method uses that address in the parameter to find an instance of the class, the specified field call passes the value of the willIChange field of the class instance to the method ClassTaker . Calling the willIChange field that does not change the structure is called in the method StructTaker because the value of the parameter is a copy of the structure, rather than copying its address. StructTaker changes the replica, so the replica is lost when the call to StructTaker completes.
class TheClass { public string willIChange; } struct TheStruct { public string willIChange; } class TestClassAndStruct { static void ClassTaker(TheClass c) { = "Changed"; } static void StructTaker(TheStruct s) { = "Changed"; } static void Main() { TheClass testClass = new TheClass(); TheStruct testStruct = new TheStruct(); = "Not Changed"; = "Not Changed"; ClassTaker(testClass); StructTaker(testStruct); ("Class field = {0}", ); ("Struct field = {0}", ); // Keep the console window open in debug mode. ("Press any key to exit."); (); } }
Output:
Class field = Changed Struct field = Not Changed