Converting Generic List to String and Vice Versa using C#

In this Blog we are going to see. How to Convert Generic List to String and Vice Versa using C#

Used Namespace:

using System;

using System.Collections.Generic;

 

Code Snippet:

/// <summary>

        /// This Converts Array to String and Vice Versa

        /// </summary>

        public void ConvertingStringToList()

        {

            //Adding item to the List

            List<string> list = new List<string>();

            list.Add("Lajapathy");

            list.Add("Arun");

 

            Console.WriteLine("List Items");

          

            //Displaying the List Value.

            foreach (string s in list)

            {

                Console.WriteLine(Environment.NewLine);

                Console.WriteLine(s);

            }

 

            //Converts List to String

            string arrayJoin = string.Join(" ", list);

 

            //Displaying the Joined List

            Console.WriteLine("Joined List :" + arrayJoin);

 

            //Convert String to List

            List<string> convertStringToList = new List<string>(arrayJoin.Split(' '));

 

            //Displaying the List Value.

            foreach (string s in convertStringToList)

            {

                Console.WriteLine(Environment.NewLine);

                Console.WriteLine(s);

            }

        }

 

OUTPUT:

Lajapathy

Arun

Joined Array: Lajapathy Arun

Lajapathy

Arun