Home >Backend Development >C#.Net Tutorial >C# method to dynamically add new elements to an array

C# method to dynamically add new elements to an array

高洛峰
高洛峰Original
2016-12-16 14:29:472392browse

Often during development, the string will be split. After getting the array, do the corresponding things!

But sometimes, the demand determines that the length of the array is not fixed, and C# arrays do not allow dynamic addition. New elements...

I have been struggling with this matter for a while!! God has a vision the day after tomorrow! There is a solution!

How to dynamically add it to the array? At this time we have to use List

Look at the following code:

string[] KTCodes = new string[0]; //机型逗号拆分  
 List<string> ktls = KTCodes.ToList();  
ktls.Add(SVKTCode);     
KTCodes = ktls.ToArray();

Such a conversion can achieve the effect of dynamically adding new elements


For more related articles on how to dynamically add new elements to C# arrays, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn