未分配的输出参数使用,C# [英] Use of unassigned out parameter, c#

查看:314
本文介绍了未分配的输出参数使用,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很简单的问题。
我为你demonstarte我的问题很简单的功能。

I have very simple problem. I made a very simple function for you to demonstarte my problem.

// //主

static void Main(string[] args)       
{
 double[,] mydouble = new double[1, 4];
 mynewMatrix(out mydouble);// call of method
}
public static void mynewMatrix(out double[,] d)
{
  for (int i = 0; i < 4; i++)
   {
    d[0,i]=i;
   }
}



//错误

// error

使用未分配的输出参数newMAt的输出参数newMAt'
必须分配到控制离开当前方法之前

Use of unassigned out parameter 'newMAt' The out parameter 'newMAt' must be assigned to before control leaves the current method

我不知道在哪里的问题。

I don't know where is problem.

推荐答案

如果阵列功能之外定义,你应该使用 REF (或没有,考虑到数组是引用类型)。 退出表示参数将在函数返回之前被初始化。使用的一些例子:

If the array is defined OUTSIDE of the function, you should use a ref (or nothing, considering the array is a reference type). out means the parameter will be initialized in the function before it returns. Some examples of use:

static void Main(string[] args)
{
    double[,] mydouble;
    mynewMatrix(out mydouble);// call of method

    double[,] mydouble2 = new double[1, 4];
    mynewMatrix2(mydouble2);// call of method

    // useless for what you want to do
    mynewMatrix3(ref mydouble2);// call of method
}

public static void mynewMatrix(out double[,] d)
{
    d = new double[1, 4];

    for (int i = 0; i < 4; i++)
    {
        d[0, i] = i;
    }
}

public static void mynewMatrix2(double[,] d)
{
    for (int i = 0; i < 4; i++)
    {
        d[0, i] = i;
    }
}

// useless for what you want to do
public static void mynewMatrix3(ref double[,] d)
{
    for (int i = 0; i < 4; i++)
    {
        d[0, i] = i;
    }
}



我要补充一点,如果你不知道是REF 和退出你可以读出的的区别问题/ 135234 /差异-REF和输出参数,在网间/ 135263#135263> REF之间进出的.NET

这篇关于未分配的输出参数使用,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆