Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# List<T>.ConvertAll in .NET 2.0

Tags:

c#

list

delegates

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. I am still relatively new to C# and would like to become more familiar with it. With that said, my question is:

How can I get the following code to work in .NET 2.0? When I check fieldList at the end of the ConvertAll operation, nothing has changed. 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.

like image 837
kzh Avatar asked Dec 22 '09 20:12

kzh


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


2 Answers

You need to assign the results of the ConvertAll method to the variable like this:

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. 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:

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:

List<String> convertedFieldList 
    = fieldList.ConvertAll<String>(delegate(String str) {
            return str.Trim().Replace(' ', '_');
        });
like image 158
Andrew Hare Avatar answered Sep 21 '22 00:09

Andrew Hare


ConvertAll doesn't change the input list. It returns a new list containing the converted stuff. By the way, you can remove the new Converter<string,string> with C# 2.0+:

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.

like image 31
mmx Avatar answered Sep 20 '22 00:09

mmx