C# convert array to list

C# convert array to list

Here is simple trick to convert an array into list

e.g.

In just 2 lines of code you can convert your array of strings into list of string

You can use new list constructor

 

string[] arrString = { "Satalaj", "More", "ASP", "Net", "C#" };
 

List<String> lstString = new List<string>(arrString);



OR

using below code you can perform operations on string before pusing the string into list.

using System.Collections.Generic;  
protected void Button1_Click(object sender, EventArgs e) 
{     
	string[] arrString = { "Satalaj", "More", "ASP", "Net", "C#" };      
	List <String> lstString = new List<string>();     
	foreach (string s in arrString)    
	{          
		lstString.Add(s);    
	}      
	foreach (string s in lstString)     
	{          
		Response.Write(s + "<br/>");    } // Here we have added an array of string collections into lists   
	} 
}

VB.net code

Protected Sub Button1_Click(sender As Object, e As EventArgs)

Dim arrString As String() = {"Satalaj", "More", "ASP", "Net", "C#"}

Dim lstString As List(Of [String]) = New List(Of String)()

For Each s As String In arrString


  lstString.Add(s)
Next

For Each s As String In lstString

  Response.Write(s + "<br/>")
Next

' Here we have added an array of string collections into lists


End Sub

'=======================================================
'Service provided by Telerik (
http://www.telerik.com/)
'Conversion powered by NRefactory.
'Built and maintained by Todd Anglin and Telerik
'=======================================================

Tags:

Tags:

Linq Convert String Array To Int ArrayC# Convert-List To ArrayC# ArrayList Search using Linq Extension OfType MethodC# convert array to listlistbox to listboxLinq Inner JoinC# Arraylist foreachConvert Array To Comma Separated String C#How To Declare and access array of string in C#One dimensional array example C#.net VB.netC# Listbox Foreach

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.