English 中文(简体)
重整一个功能中的阵列,但当它返回时,其原体规模是
原标题:Resized an array in a function but when it returned it was its original size
  • 时间:2024-05-01 02:52:44
  •  标签:
  • c#
  • .net

我通过一个阵列,在我转播阵列的单独卷宗中发挥作用。 当我返回阵列并在我的主要档案中检查其长度时,它就是以前的规模。 我绝对没有把要做的事情混为一谈。

    //this is the code in my main file
    string[] Names = new string[0];

    selectCode.namesSetup(Names);

    //and this is the code in my file where the function is located
    public string[] namesSetup(string[] Names)
    {
        Array.Clear(Names, 0, Names.Length);
        Array.Resize(ref Names, 65);

        return Names;
    }
问题回答

你们不能实际上转播一个阵列。 覆盖面积固定。 该<代码>Array.Resize方法实际上创造了一套新的固定尺寸和旧尺寸的复印件。 因此,第一个参数被宣布为<代码>ref:因为它需要提及一个比目前不同的物体。 由于你自己的<代码>名称/代码>中的参数没有被宣布为<代码>ref,因此,在方法内向该参数指定新的标的,对您的变量没有影响。

确实,你的方法实际上回到了新阵列,但你却无视返回的价值。 你们的法典可以在不改变这一方法的情况下开展工作。

Names = selectCode.namesSetup(Names);

或者,你可以改变这一方法:

public void namesSetup(ref string[] Names)
{
    Array.Clear(Names, 0, Names.Length);
    Array.Resize(ref Names, 65);
}

之后称:

selectCode.namesSetup(ref Names);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签