Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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# - Fatal编程技术网

向C#数组添加元素

向C#数组添加元素,c#,C#,我想以编程方式在C#中向字符串数组添加或删除一些元素,但仍保留以前的项,有点像VB函数。不要使用数组-使用允许动态添加项的泛型 如果这不是一个选项,您可以使用或将数组复制到更大的数组中。使用List而不是string[] 列表允许您添加和删除性能良好的项。由于数组实现了IEnumerable,您可以使用Concat: string[] strArr = { "foo", "bar" }; strArr = strArr.Concat(new string[] { "something", "ne

我想以编程方式在C#中向字符串数组添加或删除一些元素,但仍保留以前的项,有点像VB函数。

不要使用数组-使用允许动态添加项的泛型

如果这不是一个选项,您可以使用或将数组复制到更大的数组中。

使用
List
而不是
string[]


列表允许您添加和删除性能良好的项。

由于数组实现了
IEnumerable
,您可以使用
Concat

string[] strArr = { "foo", "bar" };
strArr = strArr.Concat(new string[] { "something", "new" });

或者更合适的方法是使用支持内联操作的集合类型。

您应该查看
列表。列表往往更适合按您的需要动态更改。数组不是那么多…

您可以使用一个通用集合,如列表

List List=新列表();
//加
列表。添加(“要素”);
//除去
列表。删除(“元素”);

最明显的建议是使用
列表,您已经从其他答案中阅读了该列表。这绝对是真实开发场景中的最佳方式

当然,我想让事情变得更有趣(那是我的一天),所以我会直接回答你的问题

这里有两个函数可以添加和删除
字符串[]
中的元素

string[] Add(string[] array, string newValue){
    int newLength = array.Length + 1;

    string[] result = new string[newLength];

    for(int i = 0; i < array.Length; i++)
        result[i] = array[i];

    result[newLength -1] = newValue;

    return result;
}

string[] RemoveAt(string[] array, int index){
    int newLength = array.Length - 1;

    if(newLength < 1)
    {
        return array;//probably want to do some better logic for removing the last element
    }

    //this would also be a good time to check for "index out of bounds" and throw an exception or handle some other way

    string[] result = new string[newLength];
    int newCounter = 0;
    for(int i = 0; i < array.Length; i++)
    {
        if(i == index)//it is assumed at this point i will match index once only
        {
            continue;
        }
        result[newCounter] = array[i];
        newCounter++;
    }  

    return result;
}
string[]添加(string[]数组,string newValue){
int newLength=array.Length+1;
字符串[]结果=新字符串[newLength];
for(int i=0;i
如果您真的不想(或不能)使用通用集合而不是数组,c#版本的redim preserve:

var  oldA = new [] {1,2,3,4};
Array.Resize(ref oldA,10);
foreach(var i in oldA) Console.WriteLine(i); //1 2 3 4 0 0 0 0 0 0
这一条有什么意义

List tmpList=intArry.ToList();
tmpList.Add(anyInt);

intArry=tmpList.ToArray()

您可以使用此代码段:

static void Main(string[] args)
{



        Console.WriteLine("Enter number:");
        int fnum = 0;
        bool chek = Int32.TryParse(Console.ReadLine(),out fnum);            


        Console.WriteLine("Enter number:");
        int snum = 0;
        chek = Int32.TryParse(Console.ReadLine(),out snum);


        Console.WriteLine("Enter number:");
        int thnum = 0;
        chek = Int32.TryParse(Console.ReadLine(),out thnum);


        int[] arr = AddToArr(fnum,snum,thnum);

        IOrderedEnumerable<int> oarr = arr.OrderBy(delegate(int s)
        {  
            return s;
        });


        Console.WriteLine("Here your result:");


        oarr.ToList().FindAll(delegate(int num) {

            Console.WriteLine(num);

            return num > 0;

        });



}



public static int[] AddToArr(params int[] arr) {

    return arr;
}
static void Main(字符串[]args)
{
Console.WriteLine(“输入编号:”);
int-fnum=0;
bool chek=Int32.TryParse(Console.ReadLine(),out-fnum);
Console.WriteLine(“输入编号:”);
int snum=0;
chek=Int32.TryParse(Console.ReadLine(),out-snum);
Console.WriteLine(“输入编号:”);
int-thnum=0;
chek=Int32.TryParse(Console.ReadLine(),out thnum);
int[]arr=AddToArr(fnum、snum、thnum);
IOrderedEnumerable oarr=arr.OrderBy(委托(int-s)
{  
返回s;
});
Console.WriteLine(“这里是您的结果:”);
oarr.ToList().FindAll(委托(int-num){
控制台写入线(num);
返回num>0;
});
}
公共静态int[]AddToArr(参数int[]arr){
返回arr;
}
我希望这将对您有所帮助,只需将类型更改为一行:

    string[] items = new string[] { "a", "b" };

    // this adds "c" to the string array:
    items = new List<string>(items) { "c" }.ToArray();      
string[]items=新字符串[]{“a”,“b”};
//这会将“c”添加到字符串数组中:
items=新列表(items){“c”}.ToArray();

您尝试过什么?由于这个问题的基本性质,它可能在3分钟内有50个答案。谷歌也会提供同样的帮助。vb的redim保护过去是,现在也是。毫无例外,使用集合类型总是更好。我很少会做出这样的绝对声明。对于这个问题,负5似乎有点苛刻。我在从VBA转换到C#的过程中与这个概念和转换进行了斗争。再次感谢大家花时间回答我的问题并回答了我的问题!!!!你们每个人都对我有用!!!特别感谢你,你的答案就是我一直在寻找的!!!请注意,
列表
在内部的作用与
ReDim Preserve
在数组上的作用完全相同。事实上,
List
将内容存储在内部数组中,并在需要时自动调整大小。这使得
ReDim Preserve
即使在VB中也过时了。从as列表中删除元素的等效概念可能是通过
RemoveAt
方法,因为它接受索引值<代码>删除
删除第一个可能需要但似乎更难控制的事件。
    string[] items = new string[] { "a", "b" };

    // this adds "c" to the string array:
    items = new List<string>(items) { "c" }.ToArray();