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

📄 class1.cs

📁 C#开发教程 由浅入深 配有实例 是初学者的好帮手
💻 CS
字号:
using System;

namespace RefByOut
{
	class AnotherClass
	{
		public int ID;
	}
	class SomeClass
	{
		// this won't compile
//		public AnotherClass ChangeObject(out AnotherClass ref1)
//		{
//			ref1.ID = ref1.ID*2;
//			return ref1;
//		}
		// this won't compile
//		public AnotherClass ChangeObject(out AnotherClass ref1)
//		{
//			ref1.ID = 4;
//			return ref1;
//		}
		// this won't compile
//		public AnotherClass ChangeObject(out AnotherClass ref1)
//		{
//			int x = ref1.ID;
//			ref1 = new AnotherClass();
//			ref1.ID = x * 2;
//			return ref1;
//		}

		// this WILL compile
		public AnotherClass ChangeObject(out AnotherClass ref1)
		{
			ref1 = new AnotherClass();
			ref1.ID = 99;
			return ref1;
		}
	}

	class RefByOutTest
	{
		[STAThread]
		static void Main(string[] args)
		{
			SomeClass sc = new SomeClass();
			AnotherClass ref1 = new AnotherClass();
			ref1.ID = 3;

			AnotherClass ref2 = sc.ChangeObject(out ref1);
			Console.WriteLine("ref1.ID = {0}, ref2.ID = {1}",
				ref1.ID, ref2.ID);

		}
	}
}

⌨️ 快捷键说明

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