简体   繁体   中英

c# out parameter, why I have assigned the value to my variable, still can't return?

I have a simple method like below,

        private int Increment(out n) {
            int n = 46;
            return n++;
        }

as my understanding, using out must initialise the variable first, but I still got the error of

return n++ 

The out parameter 'n' must be assigned to before control leaves the current method

I also got an error of my variable

int n = 46;

A local or parameter named 'n' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

My question is, why I can't declare a new variable inside my method, it seems I must declare my variable outside of the method, then assigned the value inside the method.

If I just out, it means I must pass variable int to my method, can't I created inside the method? And out the parameter's pointer I declare inside my method?

The out declaration 'declares' the variable, you just have to set it in the method:

private int Increment(out int n)
{
    n = 46;
    return n++;
}

Note that this method returns 46, but n is 47.

You can write your method like this. It will return perfect output.

private int Increment(out int n)
{
    n = 46;
    n++;
    return n;
}

This will return 47 as output.

Not sure what you are trying to do but it's not the way it works. Look at the example (its not the simplest way to increment a number it's just a POC of how the ref and out parameters work).

static void Main(string[] args)
{
    int incrementingNumber = 0;

    while(true)
    {
        Increment(ref incrementingNumber, out incrementingNumber);

        Console.WriteLine(incrementingNumber);
        Thread.Sleep(1000);
    }
}

private static void Increment(ref int inNumber ,out int outNumber)
{
    outNumber = inNumber + 46;
}

the output is : 46 92 138 184 230 276 322 368 414 460 506 552

  1. basically, you did not declare n correctly.
  2. then in your function (line3), as n is declared already, you cannot declare it as int again.
  3. your function will always return 46, because you are reassigning it in line 3 to 46
  4. your function will return 46, but will set n to 47, im not sure if this is what you want.

If you want to just increment by 1 you could run this Code anywhere:

n++; // increment n by 1

n += 46; // increment n by 46

If you really want a function, I doubt that you need both, out int n and return n

this code will increment your variable n:

private static void Main()
{
    int n = 46;
    // some code here
    IncrementByOne(ref n);
    Console.WriteLine("N = " + n);
}    

/// this function will increment n by 1
private static void IncrementByOne(ref int n) { n++; }

This code will return an incremented integer

private static void Main ()
{
    int n = 46;
    // some code here
    n = IncrementByOne(n);
    Console.WriteLine("N = " + n);
}    

/// this function will return an integer that is incremented by 1
private static int IncrementByOne(int n)
{
    n++; //increment n by one
    return n;
}

If you want to declare n in your function, you have to declare it earlier

static void Main(string[] args)
{
    int n = 46; // you have to set n before incrementing
    // some code here
    IncrementByOne(out n);
    Console.WriteLine("N = " + n);
}
/// this function will always set variable n to 47
private static void IncrementBy47(out int n)
{
    n = 46; // assign n to 46
    n++; // increase n by one
}

This could also be shortened like:

/// This function will always set variable n to 47
private static void SetVarTo47(out int n) { n = 47; }

Hope that helps understanding what they do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM