后来在节目中调整在C#中的数组 [英] Resize array in C# later in the program

查看:143
本文介绍了后来在节目中调整在C#中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否可能在C#
但我有中定义的变量

I am not sure if this is possible in c# but i'm having a variable defined in

 public partial class Form1 : Form
 {...
   ...
    #region used variables
    public ulong[] logP = {0,0,0,0,0,0,0,0}
  ....
  ..

再后来在我想有一个选项,在程序中调整大小的程序
之前的主要程序将启动它,并做一些计算

Then later in the program i want have an option to adjust the size in the program before the main routines will launch over it and do some calculations

因为我喜欢与vaious组数字和数组大小来测试我想有一个选项来调整阵列,每个测试(但绑定到一个单一的过程,它是一个全局变量需要被这阵心不是可调整大小的。

Because i like to test with vaious sets of numbers and array sizes i would like to have an option to resize the array, for each test (but this array isnt bound to a single procedure, its a global variable that needs to be resizable.

截至各个部分,整个程序正在使用此阵,(下各种功能),我应该怎么把这个部分

As the whole program at various parts is using this array, (under various functions) how should i put this part

    ulong [] sumP = new ulong[numericupdown.valeu];

因此​​,它会改变这个全局变量的大小?

So that it would change this global variable size ?

推荐答案

您不能调整大小的数组;您必须声明所需大小的新数组和原始数组中的内容复制到新的数组。

You cannot resize an array; you must declare a new array of the desired size and copy the contents of the original array into the new array.

更新:我不喜欢 Array.Resize - 它不调整阵列(如方法名称所暗示的),它会创建一个新的数组并取代参考:

Update: I don't like Array.Resize - it doesn't resize the array (as the method name would suggest), it creates a new array and replaces the reference:

    static void Main(string[] args)
    {
        int[] a1 = new int[] { 1, 2, 3 };
        int[] a2 = a1;

        Console.WriteLine("A1 Length: {0}", a1.Length); // will be 3
        Console.WriteLine("A2 Length: {0}", a2.Length); // will be 3
        Array.Resize(ref a1, 10);
        Console.WriteLine("A1 Length: {0}", a1.Length); // will be 10
        Console.WriteLine("A2 Length: {0}", a2.Length); // will be 3 - not good

        Console.ReadLine();
    }

这篇关于后来在节目中调整在C#中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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