Parameters passed C # based learning,

Original link: http://www.cnblogs.com/wispzone/archive/2009/07/24/1530199.html
Here are a few concepts need to be clear:
Passed by value, which is passed by reference parameter passing method in two ways, the value and reference types are two different types of systems.
You can pass type parameter value and reference values ​​passed manner.
Passing a value type parameter
Value type variable contains its data directly . Pass a value type variable to a method means passing a copy of the variable to the method. Occurred within the method has no effect on the changes to the original data stored in the variable parameters.
If you want the called method to change the value of a parameter, you must use the  ref  or  out  keyword This parameter is passed by reference.
Example 1:
class PassingValByVal
{
static  void SquareIt ( int x)
 // value type parameter passed by value 
// x changes only for a copy, without altering the original object
{
x *= x;
The System.Console.WriteLine ( "the value in the method: {0}" , X);
}
static void Main()
{
int n = 5;
The System.Console.WriteLine ( "values before calling the method: {0}" , n-);
SquareIt(n);  // Passing the variable by value.
The System.Console.WriteLine ( "value after calling the method: {0}" , n-);
 . The Console window // Open the Keep MODE Debug in 
the System.Console.WriteLine ( . "Press any key to exit" );
System.Console.ReadKey();
}
}
/ * Output:
 the value before calling the method : 5
value in the method : 25
values after calling the method : 5
* /

Pass a reference type parameter
Reference type of variable which does not directly contain data; it contains a reference to its data. When the type of the parameter values are passed by reference, it is possible to change the reference data points, such as certain members of the value (as an example arr [0] value). But it can not change the reference value itself; that is, you can not use the same reference for the new class to allocate memory and keep it (bit of a mouthful outside the block, in fact, means that a new instance can not be out on the initial target, because we usually use string direct assignment without using new so confused we .string and also other references to the same type, once assigned to reallocate memory) to do this, you should use some extent  ref  or  out  keyword pass parameters.
下面的示例演示通过值向 Change 方法传递引用类型的参数 arr。由于该参数是对 arr 的引用,所以有可能更改数组元素的值。但是,试图将参数重新分配到不同的内存位置时,该操作仅在方法内有效,并不影响原始变量 arr
class PassingRefByVal
{
static void Change(int[] pArray)
{
pArray[0] = 888;  // 改变了数组的首项
pArray = new int[5] {-3, -1, -2, -3, -4};   // 使用相同的引用分配内存
System.Console.WriteLine("方法内部,数组首项: {0}", pArray[0]);
}
static void Main()
{
int[] arr = {1, 4, 5};
System.Console.WriteLine("调用方法之前首项: {0}", arr [0]);
Change(arr);
System.Console.WriteLine("调用之后的首项: {0}", arr [0]);
}
}
/* Output:
调用方法之前首项: 1
方法内部,数组首项: -3
调用之后的首项: 888
*/
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
   
 
  

转载于:https://www.cnblogs.com/wispzone/archive/2009/07/24/1530199.html

Guess you like

Origin blog.csdn.net/weixin_30588675/article/details/94792897