Adding elements to a C# array

前端 未结 10 410
野趣味
野趣味 2021-01-11 19:29

I would like to programmatically add or remove some elements to a string array in C#, but still keeping the items I had before, a bit like the VB function ReDim Preserve.

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-11 20:05

    You can use this snippet:

    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 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;
    }
    

    I hope this will help to you, just change the type

提交回复
热议问题