【问题标题】:c# out parameter, why I have assigned the value to my variable, still can't return?c#out参数,为什么我给我的变量赋值了,还是不能返回?
【发布时间】:2019-01-30 20:22:44
【问题描述】:

我有一个简单的方法,如下所示,

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

根据我的理解,使用out必须先初始化变量,但我仍然得到了

的错误
return n++ 

必须在控制离开当前方法之前分配out参数'n'

我的变量也有错误

int n = 46;

不能在此范围内声明名为“n”的本地或参数,因为该名称在封闭的本地范围中用于定义本地或参数

我的问题是,为什么我不能在我的方法内声明一个新变量,看来我必须在方法外声明我的变量,然后在方法内赋值。

如果我刚出来,就意味着我必须将变量 int 传递给我的方法,我不能在方法内部创建吗?我在方法中声明的参数指针是什么?

【问题讨论】:

  • private int Increment(out n) 应该是private int Increment(out int n) 对吧?那么你不必写int n = 46,而是n = 46
  • 是的,对不起,我错过了,它是私有的 int Increment(out int n)。
  • 我想我错过了你的功能的目的。是否仅用于教育目的和理解out?因为这不会增加任何东西。它只是将一个变量设置为 47 并返回 46(不管你调用它多少次)。
  • @Rafalon 我只是想知道为什么我不能在方法内部声明 int n,关于变量,我认为根据我的代码输出它会返回 47。作为引用类型,输出 n 将为 47。
  • @ckky1213 任何 SO 用户都应该能够编辑他们自己的问题,如果你不能,那么这是一个错误。

标签: c# oop scope reference out


【解决方案1】:

out 声明“声明”变量,您只需在方法中设置它:

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

注意这个方法返回46,但是n是47。

【讨论】:

  • 告诉你这将返回46n设置为47
  • 不客气。如果你想更进一步,return ++n; 将返回并将 n 设置为 47
  • 我很想添加它,但我不记得那个语法叫什么:)。真的,如果该方法增加了传递的值,它应该返回 void..
  • 我想预增 (++n) 而不是后增 (n++)。你是正确的返回void,或者可能是一个错误代码
  • 初始化通常用来指assignment结合declaration。不幸的是,整个参数位是一个声明,但“out 声明声明...”不能很好地扫描。我仍然会尝试在那里找到不同的语言。
【解决方案2】:

你可以这样写你的方法。它将返回完美的输出。

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

这将返回 47 作为输出。

【讨论】:

    【解决方案3】:

    不确定您要做什么,但这不是它的工作方式。 查看示例(它不是增加数字的最简单方法,它只是 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;
    }
    

    输出是: 46 92 138 184 230 276 322 368 414 460 506 552

    【讨论】:

    • 这并没有显示任何关于 ref 的内容(您也可以在没有 ref 的情况下编写相同的内容并且仍然具有相同的输出)
    • 就像我说的 poc
    • 好吧,我刚刚意识到“poc”的意思是“一段代码”......你仍然没有展示ref 的工作原理,这与你写的相反。人们会期望代码示例显示它允许函数读取 更改传递给它的值
    【解决方案4】:
    1. 基本上,您没有正确声明 n。
    2. 然后在您的函数(第 3 行)中,由于 n 已声明,您不能再次将其声明为 int。
    3. 您的函数将始终返回 46,因为您在第 3 行将其重新分配到第 46 行
    4. 您的函数将返回 46,但会将 n 设置为 47,我不确定这是否是您想要的。

    如果您只想增加 1,您可以在任何地方运行此代码:

    n++; // increment n by 1
    
    n += 46; // increment n by 46
    

    如果你真的想要一个函数,我怀疑你需要out int nreturn 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++; }
    

    此代码将返回一个递增的整数

    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;
    }
    

    如果你想在你的函数中声明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 function will always set variable n to 47
    private static void SetVarTo47(out int n) { n = 47; }
    

    希望这有助于理解他们的工作。

    【讨论】:

    • private static void IncrementByOne(out int n) { n++; } 这是错误。它应该是ref 而不是out。 (使用未分配的参数)和IncrementByOne(n); 也应该是IncrementByOne(ref n);
    • 感谢您的补充 :),已在我的帖子中实现
    猜你喜欢
    • 2021-09-14
    • 1970-01-01
    • 2018-04-22
    • 2018-11-12
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    相关资源
    最近更新 更多