简体   繁体   English

C#输出参数,为什么我给我的变量赋值,仍然无法返回?

[英]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 必须在控制离开当前方法之前将out参数'n'分配给

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 无法在此范围内声明名为“ n”的本地或参数,因为该名称在封闭的本地范围内用于定义本地或参数

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? 如果我只是出去,那意味着我必须将变量int传递给我的方法,我不能在方法内部创建吗? 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: out声明“声明”了变量,您只需要在方法中进行设置即可

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

Note that this method returns 46, but n is 47. 请注意,此方法返回46,但n为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. 这将返回47作为输出。

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). 看一下示例(这不是递增数字的最简单方法,它只是refout参数的工作原理的POC)。

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 输出为:46 92 138 184 230 276 322 368 414 460 506 552

  1. basically, you did not declare n correctly. 基本上,您没有正确声明n。
  2. then in your function (line3), as n is declared already, you cannot declare it as int again. 然后在函数(第3行)中,由于已经声明了n ,因此无法再次将其声明为int。
  3. your function will always return 46, because you are reassigning it in line 3 to 46 您的函数将始终返回46,因为您正在第3到46行中对其进行重新分配
  4. your function will return 46, but will set n to 47, im not sure if this is what you want. 您的函数将返回46,但会将n设置为47,我不确定这是否是您想要的。

If you want to just increment by 1 you could run this Code anywhere: 如果只想增加1,则可以在任何地方运行此代码:

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 如果您真的想要一个函数,我怀疑您是否需要out int nreturn n

this code will increment your variable n: 此代码将使变量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 如果要在函数中声明n,则必须提前声明

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. 希望有助于理解他们的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将wchar_t *从C ++编组为C#作为out参数或返回值? - How do I marshal wchar_t* from C++ to C# as an out parameter or return value? 为什么 out 参数不能有默认值? - Why can't an out parameter have a default value? 在C#4.0中,为什么方法中的out参数不能协变? - In C# 4.0 why can't an out parameter in a method be covariant? 当我清除以前分配给它的列表时,C#词典值就会清除……为什么? - C# dictionary value clearing out when I clear list previously assigned to it…why? Unity/C#:我不知道为什么这个变量在当前上下文中不存在 - Unity/C#: I can not work out why this variable doesn't exist in the current context 为什么我不能用T? 作为 C# 中的泛型参数 - Why I can't use T? as a generic parameter in C# 是否可以返回char或字符串的c#方法的返回值或out参数? - Return value or out parameter for c# method that can return either a char or string? 我无法弄清楚为什么我的文本框没有在asp.net中触发我的OnClick c#函数 - I can't figure out why my textbox isn't firing my OnClick c# function in asp.net 布尔变量更改值,但我不知道为什么 - Bool variable changing value, but I can't figure out why c# 为什么我不能从最小化返回我的应用程序? - c# Why can I not return my application from minimize?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM