⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cs_array_ref.cs

📁 java基础方面的一些实例代码
💻 CS
字号:
// cs_array_ref.cs
using System; 
class TestRef 
{
	public static void FillArray(ref int[] arr) 
	{
		arr[0] = 123;
		arr[4] = 1024;
	}
	//对于引用类型,例如数组,不用ref也可以传出方法中改变的参数值,前提是在方法中不能用new初始化形参
	public static void FillArray_notRef(int[] arr) 
	{
		arr[0] = 1111;
		arr[4] = 2222;
	}

	public static void FillArray1(ref int[] arr) 
	{
		arr = new int [] {11,22,33,44,55};  //将会改变实参的值
		arr[0] = 123;
		arr[4] = 1024;
	}
	//对于引用类型,例如数组,
	public static void FillArray_notRef1(int[] arr) 
	{
		arr = new int [] {111,222,333,444,555}; //将不会改变实参的值,与实参无关
		arr[0] = 1111;
		arr[4] = 2222;
	}

	//对于引用类型,例如string,
	public static void string_notRef(string str) 
	{
		str="abcde";   //与实参无关,不能改变实参
	}

	//对于引用类型,例如string,
	public static void string_Ref(ref string str) 
	{
		str="abcde";  //改变实参
	}

	static public void Main () 
	{
		// Initialize the array:
		int[] myArray = {1,2,3,4,5};  //调用方法前必须初始化 

		Console.WriteLine("ref ====================");
		// Pass the array using ref:
		FillArray(ref myArray);
		// Display the updated array:
		Console.WriteLine("Array elements are:");
		for (int i = 0; i < myArray.Length; i++) 
			Console.WriteLine(myArray[i]);

		Console.WriteLine("Not ref ====================");
		// Pass the array Not using ref:
		FillArray_notRef(myArray);
		// Display the updated array:
		Console.WriteLine("Array elements are:");
		for (int i = 0; i < myArray.Length; i++) 
			Console.WriteLine(myArray[i]);

		Console.WriteLine("ref new ====================");
		// Pass the array  using ref:
		FillArray1(ref myArray);
		// Display the updated array:
		Console.WriteLine("Array elements are:");
		for (int i = 0; i < myArray.Length; i++) 
			Console.WriteLine(myArray[i]);

		Console.WriteLine("Not ref new ====================");
		// Pass the array Not using ref:
		FillArray_notRef1(myArray);
		// Display the updated array:
		Console.WriteLine("Array elements are:");
		for (int i = 0; i < myArray.Length; i++) 
			Console.WriteLine(myArray[i]);

		Console.WriteLine("Not ref string ====================");
		string mystr="1234";
		string_notRef(mystr);
		Console.WriteLine(mystr);
		string_Ref(ref mystr);
		Console.WriteLine(mystr);


		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -