Showing posts with label C# list. Show all posts
Showing posts with label C# list. Show all posts

C# convert array to list

In this post we will se how to convert array to list. We need to convert array object to generic list many times in C# programming. Converting array to list can be done by various ways. We are going to see C# examples to convert array to the list.

Conversion of array to an list can be done using constructor of the generic List class. We here use constructor of List and pass array object as parameter to List constructor and then convert it to List>

c# array to list
.

Example - convert array to list [C#]:

using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
// String array to convert to list
 string[] strarr = new string[]
 {
  "Convert",
  "Array",
  "To",
  "list",
 };

//Now we will convert above array using new list constructor.
 List<string> arrayToList = new List<string>(strarr);
}

In above example we saw how we converted array to list using List constructor. Now we will see other way to convert Array to list. In this way we use .ToList() instance method that converts array to list. Below example shows how array get converted in to list using .ToList() parameter less instance method.

Example to convert array to list [C#]:

using System;
using System.Collections.Generic;

class Program
{    
static void Main()
{    
// String array to convert to list    
 string[] strarr = new string[]    
 {
  "Convert",
  "Array",
  "To",
  "List",
 };
//Now we will convert above array using .ToList instance method.
List<string> arrayToList = strarr.ToList();
}

C# sort list [Generic List<T>]

c# sort list
Generic List in C# provides easy way to sort list. In C# some times we need to add data to generic list and sort list by ascending based on our need. C# .net provides .sort method for List that can be used for sorting the contents of list. We will see example to sort list in c#.

Sample code to sort list [C#]:


using System;
using System.Collections.Generic;

public class sortlist
{
public static void Main()
{
List<string> names =  new List<string>();
names.Add("John");
names.Add("Antony");
names.Add("Mickel");
names.Add("Daniel");
names.Sort();
Console.WriteLine();
foreach(string name in names)
{
Console.WriteLine(name);
}
}
}


//Output
//Antony
//Daniel
//John
//Mickel

List to array c#

array to list
In this post we will se how to convert list to array.We need to convert list to array many times in C# programming. Converting list to array can be done by various ways. We are going to see C# examples to convert the list to array.

we will see the way to convert list to an array. we use .ToArray() parameter less instance method that converts list to an array. Below example shows how list get converted in to array using .ToArray() method:

Example to convert list to array [C#]

using System;
using System.Collections.Generic;

class Program
{
 static void Main()
 {
  // List to convert into Array.
  List<string> lstString = new List<string>();
  lstString.Add("Convert");
  lstString.Add("List");
  lstString.Add("TO");
  lstString.Add("Array");
  // use .ToArray() Method to convert list to array
  string[] strArray = lstString.ToArray();
 }
}

Array to List [C#]

convert array to list

Some times we need add elements of an array to list in C# programming. We can use .Add() method of List class to add array elements to the List.Using . we also can use .Add() method to while converting array to List.

We will see example that will show how used .Add method to convert array to List. Here we iterate through array elements and then one by one we add those elements to List.



using System.Collections.Generic; 

protected void Button1_Click(object sender, EventArgs e) 
{     
 string[] arrStr = { "Add", "Array", "Elements", "To", "List", "C#" };      
 List <string> strList = new List<string>();     
 foreach (string stritem in arrStr)    
 {          
  lstString.Add(stritem);    
 }      
}