本文介绍了为什么我在C#中遇到这些参数错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手.我已经在C#中尝试了out参数

I am new to C#. I've tried this with out parameter in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class First
{
    public void fun(out int m)
    {
        m *= 10;
        Console.WriteLine("value of m = " + m);
    }
}

class Program
{
    static void Main(string[] args)
    {
        First f = new First();
        int x = 30;
        f.fun(out x);
    }
}

但是我遇到一些错误,例如"使用未分配的参数'm'"
必须在控制离开当前方法之前将out参数'm'分配给它.

but i get some errors like "Use of unassigned out parameter 'm'" and
The out parameter 'm' must be assigned to before control leaves the current method.

那么这些错误的含义是什么,为什么在我已经为 x 分配了值的情况下必须强制分配' m '.

So what is the meaning of these errors and why it is compulsory to assign 'm' when i'm already assigned a value to x.

推荐答案

ref 意味着您正在传递对已声明 并已初始化 ,然后再调用该方法,该方法可以修改该变量的值.

ref means that you are passing a reference to the variable that has been declared and initialized, before calling the method, and that the method can modify the value of that variable.

out 表示您在调用该方法之前将引用传递给已声明但 尚未初始化 的变量,并且该方法必须在返回之前初始化或设置其值.

out means you are passing a reference to the variable that has been declared but not yet initialized, before calling the method, and that the method must initialize or set it's value before returning.

这篇关于为什么我在C#中遇到这些参数错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:34