裁判"实际"的例子;使用 [英] Example of practical of "ref" use
本文介绍了裁判"实际"的例子;使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我奋力如何使用REF(通过引用参数)在真正的应用程序。我想有简单而有意义的主要例子。一切我迄今发现可能与添加返回类型的方法很容易地重做。
任何想法的人?
谢谢
I am struggling how to use "ref" (to pass argument by reference) in real app. I would like to have simple and mainly meaningful example. Everything I found so far could be easily redone with adding return type to the method. Any idea someone? Thanks!
推荐答案
在我心中未来最好的例子是交换两个变量值的函数:
The best example coming in my mind is a function to Swap two variables values:
static void Swap<T>(ref T el1, ref T el2)
{
var mem = el1;
el1 = el2;
el2 = mem;
}
用法:
Usage:
static void Main(string[] args)
{
string a = "Hello";
string b = "Hi";
Swap(ref a, ref b);
// now a = "Hi" b = "Hello"
// it works also with array values:
int[] arr = new[] { 1, 2, 3 };
Swap(ref arr[0], ref arr[2]);
// now arr = {3,2,1}
}
类似这样的功能,不能没有 REF
关键字来完成。
这篇关于裁判"实际"的例子;使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文