首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

将List(of object)转换为List(of string)

将 List(of object) 转换为 List(of string) 的方法是使用 LINQ 查询。以下是一个简单的示例,说明如何将 List(of object) 转换为 List(of string):

代码语言:csharp
复制
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        List<object> listOfObjects = new List<object> { "Apple", "Banana", "Orange", 1, 2.0, 'A' };

        List<string> listOfStrings = listOfObjects
            .Where(x => x.GetType() == typeof(string))
            .Select(x => (string)x)
            .ToList();

        foreach (string str in listOfStrings)
        {
            Console.WriteLine(str);
        }
    }
}

在这个示例中,我们首先创建了一个包含不同类型的对象的 List(of object)。然后,我们使用 LINQ 查询筛选出类型为字符串的对象,并将其转换为 List(of string)。最后,我们遍历 List(of string) 并输出每个字符串。

这个示例中的代码可以很好地处理包含不同类型对象的 List(of object),并将其转换为 List(of string)。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券