Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# list to string

List<string> names = new List<string>() { "John", "Anna", "Monica" };
string result = string.Join(",", names.ToArray());
Comment

c# convert list to string

string combindedString = string.Join( ",", myList.ToArray() );
Comment

list of string to string c#

string Something = string.Join(",", MyList);
Comment

c# list to string

using System.Linq;
string str = list.Aggregate((x, y) => x + ',' + y);
Comment

c# convert list to string and back

public static class UsersConverter
{
    // Separates user properties.
    private const char UserDataSeparator = ',';

    // Separates users in the list.
    private const char UsersSeparator = ';';

    public static string ConvertListToString(IEnumerable<User> usersList)
    {
        var stringBuilder = new StringBuilder();

        // Build the users string.
        foreach (User user in usersList)
        {
            stringBuilder.Append(user.Name);
            stringBuilder.Append(UserDataSeparator);
            stringBuilder.Append(user.Age);
            stringBuilder.Append(UsersSeparator);
        }

        // Remove trailing separator.
        stringBuilder.Remove(stringBuilder.Length - 1, 1);

        return stringBuilder.ToString();
    }

    public static List<User> ParseStringToList(string usersString)
    {
        // Check that passed argument is not null.
        if (usersString == null) throw new ArgumentNullException("usersString");

        var result = new List<User>();

        string[] userDatas = usersString.Split(UsersSeparator);

        foreach (string[] userData in userDatas.Select(x => x.Split(UserDataSeparator)))
        {
            // Check that user data contains enough arguments.
            if (userData.Length < 2) throw new ArgumentException("Users string contains invalid data.");

            string name = userData[0];
            int age;

            // Try parsing age.
            if (!int.TryParse(userData[1], out age))
            {
                throw new ArgumentException("Users string contains invalid data.");
            }

            // Add to result list.
            result.Add(new User { Name = name, Age = age });
        }

        return result;
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to show a first item in a combobox in c# winforms 
Csharp :: unity screen size fix 
Csharp :: create stripe subscription pay_immediately 
Csharp :: C# Console font 
Csharp :: hashtable in c# 
Csharp :: get list of constants in class c# 
Csharp :: delete items in c# 
Csharp :: c# return values 
Csharp :: dbset syntax 
Csharp :: Default property value in C# 
Csharp :: restrictions 
Csharp :: How to create a class and objects in C# 
Csharp :: Proxy in Config 
Csharp :: c# how to return a function 
Csharp :: change canvas color uwp c# 
Csharp :: create app() import vue cli 
Csharp :: ExpandoObject Add PropertyName and PropertyValue Dynamically 
Csharp :: create anchor tag dynamically c# 
Csharp :: c# get program version 
Csharp :: deferred rendering unity 
Csharp :: infinit range loop c# 
Csharp :: ef6 export update 
Csharp :: close an open form when you open it again c# 
Csharp :: delegate 
Csharp :: array of objects c# 
Csharp :: != in f# 
Csharp :: quick watch in visual studio 
Csharp :: finding holydays asp.net 
Csharp :: creating an object 
Csharp :: unittest servicector automapper 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =