Example of practical of "ref" use [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
开发者_如何学Python Improve this questionI 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}
}
A function like this one, cannot be done without the ref
keyword.
One possibly corner-case example: Interlocked.Increment
. Without passing the variable by reference, there's no way of performing the increment atomically.
I can't say I've used ref
very much myself, to be honest - I generally steer clear of the need to return multiple values, and even then out
is usually sufficient. Many of the cases where I've seen ref
used, it's been due to the author not understanding how arguments are passed in .NET when it comes to reference types.
The TryParse
methods built into the framework are typical examples. They use out
instead of ref
but it is the same semantics, it's just that the caller doesn't need to initialize the value. Example:
int result;
bool isSuccess = int.TryParse("some string", out result);
if (isSuccess)
{
// use the result here
}
As you can see the function returns a boolean indicating whether the operation succeeds but the actual result is returned as out
parameter.
public static void Main(string args[])
{
int i=0;
AddSomething(ref i);
AddSomething(ref i);
AddSomething(ref i);
AddSomething(ref i);
string mystr = "Hello";
AddSomeText(ref mystr);
Console.WriteLine(mystr);
Console.WriteLine("i = {0}", i);
}
public static void AddSomeText(ref string str)
{
str+= " World!";
}
public static void AddSomething(ref int ii)
{
ii+=1;
}
One of the most common places I see it, is in Save methods of some frameworks.
The reason is that in many cases it is not actually possible to maintain the same object over a save call, if the object is being serialized to another machine and then comes back as a new instance (perhaps with some extra defaults). In that case you need the ref to make it obvious that the original reference is no longer valid.
As for its NECESSITY, I can't come up with an example where it would be required. Most places out is just fine.
I think a good example would be trampolining.
This is where you take a recursive function and rework it to a method that is repeatidly called on a set of values. The reason being that instead of going into the stack very deeply the stack remains flat because you return after every call instead of calling yourself.
Regards GJ
精彩评论