开发者

Using out keyword in c#

开发者 https://www.devze.com 2023-04-11 20:25 出处:网络
can anyone suggest me the exact use of out keyword as a paramter, and how its connected for returning multiple values from the fu开发者_如何学Pythonnction, as in this POST, i am confused with out vari

can anyone suggest me the exact use of out keyword as a paramter, and how its connected for returning multiple values from the fu开发者_如何学Pythonnction, as in this POST, i am confused with out variable with normal variable. can anyone help me for this.


This is frequently confusing, and I think the MSDN documentation actually is a bit "clear only if already known". That is, it is correct, but it really only makes sense if you already understand the concept.

Here's how I think of it.

A regular parameter makes a copy of the value of the argument. When you say:

static int M(int z) { z = z + 1; return z; }    
...
int x = 123;
int y = M(x);

That is just like you said:

int x = 123;
int z = x; // make a copy of x
z = z + 1;
int y = z;

A ref or out parameter make an alias for an existing variable. When you say

static void N(ref int q) { q = q + 1; }    
...
int x = 123;
N(x);

That is the same as saying:

int x = 123;
// MAGIC: q is now an another name for variable x
q = q + 1;

q and x are two different names that refer to the same variable. Incrementing q also increments x because they are the same. z and x in the previous example are two different names that refer to two different variables. Incrementing z does not change x.

Summing up: "out" and "ref" just mean "do not make a new variable; rather, temporarily make a second name for an existing variable".

Is that now clear?

UPDATE: I did not say what the difference between "out" and "ref" is. The difference is simple. On the "caller" side, a "ref" must be a definitely assigned variable before the method is called. An "out" need not be. On the "callee" side, a "ref" may be read before it is written to, but an "out" must be written to before it is read. Also, an "out" must be written to before control leaves the method normally.


MSDN documentation already does a great job explaining this:

The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword. For example:

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}


It's very frequently used in a pattern that "tries" to get a value, something like:

int result;
if(Int32.TryParse("123", out result))
{
   Console.WriteLine(result + 1);
}


out keyword should be used when you want to: a) Allow your function to modify specific variable from calling code stack AND b) enforce setting this variable value inside your function


MSDN is always a good place to start


In most languages c# included you can pass values in 2 ways, by value, by reference.

by value gives the method a copy of your data, so changing the data wont have any effect on the original data

by reference essentially gives the method the memory address of your data, so if the method modifies the data, it changes the original.

Out is a special type of ref, in that you do not need to initialise the variable before you call the method, it can be called with null being passed in. and it MUST be set by the method.

Another way you can think of it (from the outside code's point of view) is:

val = read only

ref = read/write

out = write only.


http://msdn.microsoft.com/en-us/library/t3c3bfhx(v=vs.80).aspx

out keyword is good if you want to return multiple values of pre-defined types (for example an int, a List<string> and a DateTime), and you don't want to create a new class just for this purpose.


Ok,

let look at the usual pattern for this kind of function - the TrySomething. Suppose you have a function that might succeed giving you an value or not but you don't won't to use an exception for this because you don't want the overhead or it's a common trait. Then you normaly return true if the method suceeded and false if not. But where would you put your outputvalue to?

One possible answer is using an out parameter like this:

bool TrySomething(MyInputType input, out MyOutputType output)
{
   output = default(MyOutputType);
   /* ... Try getting the answer ... */
   if (!successful)
      return false;

   output = successfulOutput;
   return true;
}

Remark: Or you might consider using a Tuple<bool,MyOutputType> and indeed F# interpretes the pattern above as resulting in such a tuple by itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号