Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用输出参数有条件地创建新集合_C#_Collections - Fatal编程技术网

C# 使用输出参数有条件地创建新集合

C# 使用输出参数有条件地创建新集合,c#,collections,C#,Collections,本程序仅用于说明目的。我想将一个整数作为输入,如果它大于零,则在ArrayList中使用该整数创建一个ArrayList。我尝试了很多(不正确的)方法,最终确定了下面的内容。然而,我真的不喜欢它的外观 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; class Program { stat

本程序仅用于说明目的。我想将一个整数作为输入,如果它大于零,则在ArrayList中使用该整数创建一个ArrayList。我尝试了很多(不正确的)方法,最终确定了下面的内容。然而,我真的不喜欢它的外观

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter in a number of the size of the ArrayList that you want: ");
            int arraySize = int.Parse(Console.ReadLine());
            if (arraySize > 0)
            {
                ArrayList newList = CreateList(arraySize,out newList);
                newList.Add(arraySize);
                Console.WriteLine("the size of the array is {0}",newList.Count);
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("You did not create an ArrayList");
            }
            Console.WriteLine("here you can't access the array");
            Console.ReadLine();




        }
        public static ArrayList CreateList(int x,out ArrayList outList)
        {
            ArrayList al = new ArrayList(x);
            outList= al;
            return outList;

        }
    }
我的想法是,如果用户决定某个动作,那么程序就不会创建一个ArrayList来节省资源(考虑到这个例子,我知道这是一个愚蠢的例子)。但是,我仍然需要在main方法中初始化ArrayList,如果我取出行
newList.Add(arraySize)
如果用户输入大于0的数字,程序将以0的输出运行。有没有办法使任何大于0的输入都会生成一个ArrayList,该ArrayList可以与添加的元素一起使用?因此,如果我们注释掉了行
newList.Add(arraySize)
,程序仍然会将ArrayList的大小打印为1(前提是该数字大于0)


在本例中,元素的数量最多为1,而不是用户可能输入的数量。
ArrayList
中的Int参数是“初始容量”,这是一种性能特性(添加项目时,列表在达到最大容量之前不会调整大小),因此它不会影响列表的实际大小。如果要返回包含一个项目的列表,请将该项目添加到
CreateList
函数中。

ArrayList中的Int参数是“初始容量”,这是一种性能特性(添加项目时,列表的大小不会调整,直到达到最大容量为止),因此它不会影响列表的实际大小。如果要返回包含一个项目的列表,请将该项目添加到
CreateList
函数中。

一条注释: 您不必为函数设置返回类型,请尝试以下操作:

ArrayList newList =null;
CreateList(arraySize,out newList);


public static void CreateList(int x,out ArrayList outList)
{
   outList = new ArrayList(x);
}
有一条评论: 您不必为函数设置返回类型,请尝试以下操作:

ArrayList newList =null;
CreateList(arraySize,out newList);


public static void CreateList(int x,out ArrayList outList)
{
   outList = new ArrayList(x);
}

这里有很多问题

首先,您不能添加列表的大小。它在构造函数中初始化,然后随着元素的添加和删除而增大和缩小

所以这句话:

newList.Add(arraySize);
是误导性的,几乎肯定不是你想要的。您要么想添加一个元素(在这种情况下,变量名是误导性的),要么想设置数组的大小或容量,在这种情况下,代码并不是您想要的

列表的大小和容量之间也存在差异,您可能需要注意:

  • Size是数组中的元素数
  • Capacity是列表基础数组的大小,在任何时间点都将等于或大于列表的大小
这就是当您将列表初始化为其他值时,对Count的调用返回0的原因。Count指的是列表中的项数,而不是内部数组的长度。您想调用Capacity属性:

Console.WriteLine("the size of the array is {0}", newList.Capacity);
列表(ArrayList和List)在内部使用固定大小的数组。所以容量是指数组的长度。当您将新元素添加到数组已满的列表中时,列表的add方法将创建一个新数组,该数组的长度是旧数组的两倍,将所有元素复制到较大的数组中,在末尾追加新值,并丢弃旧数组

简而言之,功能:

public static ArrayList CreateList(int x,out ArrayList outList) {
    ArrayList al = new ArrayList(x);
    outList= al;
    return outList;
}
基本上是可以的,尽管有些冗余,因为它应该使用out参数或返回列表,而不是两者都返回。对于您的情况,我会做如下操作:

public static void CreateList<T>(int capacity, out List<T> list) {
    list = new List<T>(capacity);
}
publicstaticvoidcreatelist(int-capacity,out-List){
列表=新列表(容量);
}

尽管这是一个毫无意义的方法。

这里有很多问题

首先,您不能添加列表的大小。它在构造函数中初始化,然后随着元素的添加和删除而增大和缩小

所以这句话:

newList.Add(arraySize);
是误导性的,几乎肯定不是你想要的。您要么想添加一个元素(在这种情况下,变量名是误导性的),要么想设置数组的大小或容量,在这种情况下,代码并不是您想要的

列表的大小和容量之间也存在差异,您可能需要注意:

  • Size是数组中的元素数
  • Capacity是列表基础数组的大小,在任何时间点都将等于或大于列表的大小
这就是当您将列表初始化为其他值时,对Count的调用返回0的原因。Count指的是列表中的项数,而不是内部数组的长度。您想调用Capacity属性:

Console.WriteLine("the size of the array is {0}", newList.Capacity);
列表(ArrayList和List)在内部使用固定大小的数组。所以容量是指数组的长度。当您将新元素添加到数组已满的列表中时,列表的add方法将创建一个新数组,该数组的长度是旧数组的两倍,将所有元素复制到较大的数组中,在末尾追加新值,并丢弃旧数组

简而言之,功能:

public static ArrayList CreateList(int x,out ArrayList outList) {
    ArrayList al = new ArrayList(x);
    outList= al;
    return outList;
}
基本上是可以的,尽管有些冗余,因为它应该使用out参数或返回列表,而不是两者都返回。对于您的情况,我会做如下操作:

public static void CreateList<T>(int capacity, out List<T> list) {
    list = new List<T>(capacity);
}
publicstaticvoidcreatelist(int-capacity,out-List){
列表=新列表(容量);
}

尽管这是一个毫无意义的方法。

因为,很难从你的解释中理解问题是什么,我要在黑暗中尝试一下。这就是你想要的:

using System;
using System.Collections;

namespace SO16071463
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter in a number of the size of the ArrayList that you want: ");
            int arraySize = int.Parse(Console.ReadLine());
            if (arraySize > 0)
            {
                ArrayList newList = new ArrayList { arraySize };
                Console.WriteLine("the size of the array is {0}", newList.Count);
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("You did not create an ArrayList");
            }
            Console.WriteLine("here you can't access the array");
            Console.ReadLine();
        }
    }
}

因为,很难从你的解释中理解问题是什么,我要在黑暗中开枪。这就是你想要的:

using System;
using System.Collections;

namespace SO16071463
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter in a number of the size of the ArrayList that you want: ");
            int arraySize = int.Parse(Console.ReadLine());
            if (arraySize > 0)
            {
                ArrayList newList = new ArrayList { arraySize };
                Console.WriteLine("the size of the array is {0}", newList.Count);
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("You did not create an ArrayList");
            }
            Console.WriteLine("here you can't access the array");
            Console.ReadLine();
        }
    }
}
不要