简体   繁体   English

C#列表<T> .NET 2.0 中的 .ConvertAll

[英]C# List<T>.ConvertAll in .NET 2.0

I have a list of strings.我有一个字符串列表。 All of the strings have whitespace that needs to be converted to underscores.所有字符串都有需要转换为下划线的空格。 I am fully capable of using a for or foreach loop to do this.我完全有能力使用forforeach循环来做到这一点。 I am still relatively new to C# and would like to become more familiar with it.我对 C# 还是比较陌生,希望能更加熟悉它。 With that said, my question is:话虽如此,我的问题是:

How can I get the following code to work in .NET 2.0?如何让以下代码在 .NET 2.0 中工作? When I check fieldList at the end of the ConvertAll operation, nothing has changed.当我在ConvertAll操作结束时检查fieldList时,没有任何变化。 Is there an issue with passing the string by value instead of reference?按值而不是引用传递字符串是否存在问题?

string fields =
  "First Name,Middle Name,Last Name,Birth Date,Gender,Address,City,State,Zip,Email";
List<string> fieldList = new List<string>(fields.Split(','));
fieldList.ConvertAll<string>(new Converter<string, string>(
    delegate(string str)
    {
        str = str.Trim();
        str = str.Replace(' ', '_');
        return str;
    }
));

Please, keep in mind, that I am using .NET 2.0 and cannot currently switch, so I do not have the luxury of using LINQ or Lambdas.请记住,我使用的是 .NET 2.0 并且目前无法切换,所以我没有使用 LINQ 或 Lambdas 的奢侈。

You need to assign the results of the ConvertAll method to the variable like this:您需要将ConvertAll方法的结果分配给变量,如下所示:

fieldList = fieldList.ConvertAll<string>(new Converter<string, string>(
    delegate(string str)
    {
        str = str.Trim();
        str = str.Replace(' ', '_');
        return str;
    }
));

The ConvertAll method returns a new List<T> so you need to assign the result of the method. ConvertAll方法返回一个新的List<T>因此您需要分配该方法的结果。 If you want to re-use the fieldList variable you can but it may be better to create a new variable to improve the clarity of your code:如果您想重新使用fieldList变量,您可以,但最好创建一个新变量以提高代码的清晰度:

List<String> convertedFieldList 
    = fieldList.ConvertAll<string>(new Converter<string, string>(
        delegate(string str)
        {
            str = str.Trim();
            str = str.Replace(' ', '_');
            return str;
        }
));

As Marc Gravell points out in a comment below, you can simplify the syntax of this expression by doing this:正如 Marc Gravell 在下面的评论中指出的那样,您可以通过执行以下操作来简化此表达式的语法:

List<String> convertedFieldList 
    = fieldList.ConvertAll<String>(delegate(String str) {
            return str.Trim().Replace(' ', '_');
        });

ConvertAll doesn't change the input list. ConvertAll不会更改输入列表。 It returns a new list containing the converted stuff.它返回一个包含转换内容的新列表。 By the way, you can remove the new Converter<string,string> with C# 2.0+:顺便说一句,您可以使用 C# 2.0+ 删除new Converter<string,string>

List<string> converted = fieldList.ConvertAll<string>
    (delegate(string s) { return s.Trim().Replace(' ', '_'); });

Besides, nothing prevents you from using a C# 3.0 compiler and LINQBridge and target .NET 2.0.此外,没有什么可以阻止您使用 C# 3.0 编译器和LINQBridge以及目标 .NET 2.0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM