List<string>和string[]之间的相互转换

本文介绍了如何在C#中实现System.String[]与List&lt;System.String&gt;之间的相互转换。具体包括:1) 如何将字符串数组转换为列表;2) 如何将列表转换为字符串数组。这些操作在日常开发中十分常见。

1.从System.String[]转到List<System.String>

            List<System.String> List = new List<System.String>();
            string[] str={"1","2","3"};
            List = new List<System.String>(str);

 

2.从List<System.String>转到System.String[]

	   List<System.String> List = new List<System.String>();
            List.Add("1");
            List.Add("2");
            List.Add("3");
            System.String[] str = { };
            str = List.ToArray();

### 方法概述 在 C# 中,`List<T>` `ObservableCollection<T>` 是两种不同的泛型集合类型。由于 `ObservableCollection<T>` 继承自 `Collection<T>` 并实现了 `INotifyCollectionChanged` 接口,因此它非常适合用于 WPF 数据绑定场景下的动态更新需求。将 `List<string>` 转换为 `ObservableCollection<string>` 的常见方法有以下几种。 --- ### 方法一:通过构造函数初始化 可以直接使用 `ObservableCollection<T>` 的构造函数来创建新的实例并传入现有的 `List<string>`。 ```csharp using System.Collections.Generic; using System.Collections.ObjectModel; // 原始列表 List<string> originalList = new List<string> { "Apple", "Banana", "Cherry" }; // 使用构造函数转换 ObservableCollection<string> observableCollection = new ObservableCollection<string>(originalList); ``` 这种方法简单高效,适用于大多数情况[^1]。 --- ### 方法二:手动遍历添加 如果需要更灵活的操作(例如过滤或修改元素),可以选择逐一遍历原始列表并将每个项添加到目标集合中。 ```csharp using System.Collections.Generic; using System.Collections.ObjectModel; // 原始列表 List<string> originalList = new List<string> { "Apple", "Banana", "Cherry" }; // 创建目标集合 ObservableCollection<string> observableCollection = new ObservableCollection<string>(); // 手动遍历添加 foreach (string item in originalList) { observableCollection.Add(item); } ``` 这种方式虽然稍显冗长,但在某些复杂逻辑下非常有用[^2]。 --- ### 方法三:使用 LINQ 的扩展方法 借助 LINQ 提供的 `.ForEach()` 方法,可以简化遍历过程。 ```csharp using System.Collections.Generic; using System.Collections.ObjectModel; // 原始列表 List<string> originalList = new List<string> { "Apple", "Banana", "Cherry" }; // 创建目标集合 ObservableCollection<string> observableCollection = new ObservableCollection<string>(); // 使用 ForEach 添加 originalList.ForEach(item => observableCollection.Add(item)); ``` 此方法与手动遍历类似,但语法更加简洁[^3]。 --- ### 注意事项 1. **性能考虑** 当处理大量数据时,推荐直接使用构造函数的方式,因为它内部进行了优化以减少多次触发集合更改通知。 2. **只读属性的情况** 如果目标集合被定义为只读属性(即只有 getter 没有 setter),则无法直接替换整个集合对象。此时应清空现有集合并通过逐一添加的方式来完成更新[^4]。 --- ### 示例代码总结 以下是三种方法的完整示例: ```csharp using System; using System.Collections.Generic; using System.Collections.ObjectModel; class Program { static void Main() { // 初始化原始列表 List<string> originalList = new List<string> { "Red", "Green", "Blue" }; // 方法一:构造函数 ObservableCollection<string> collection1 = new ObservableCollection<string>(originalList); Console.WriteLine("Method 1 Result:"); PrintCollection(collection1); // 方法二:手动遍历 ObservableCollection<string> collection2 = new ObservableCollection<string>(); foreach (string item in originalList) { collection2.Add(item); } Console.WriteLine("\nMethod 2 Result:"); PrintCollection(collection2); // 方法三:LINQ ForEach ObservableCollection<string> collection3 = new ObservableCollection<string>(); originalList.ForEach(item => collection3.Add(item)); Console.WriteLine("\nMethod 3 Result:"); PrintCollection(collection3); } private static void PrintCollection(ObservableCollection<string> collection) { foreach (string item in collection) { Console.WriteLine(item); } } } ``` 运行结果: ``` Method 1 Result: Red Green Blue Method 2 Result: Red Green Blue Method 3 Result: Red Green Blue ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值