Array Collections File String Windows VB.NET Algorithm ASP.NET Cast Class Compression Convert Data Delegate Directive Enum Exception If Interface Keyword LINQ Loop Method .NET Number Regex Sort StringBuilder Struct Switch Time Value

A List is not an array. This is not a problem unless you need an array. Conversions are possible for both simple values and complex data types. These conversions are sometimes complicated and involved. They rely on the C# language type system.
Overview: These C# examples show how to convert types. These conversions are more complex than casts.
The C# language features many types, and sometimes you must change one type, such as an array, to another type, such as a List. In this example program, we use the System.Linq namespace to convert an int[] array into a List<int>.
Program that converts array to List [C#]
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
int[] array = { 1, 2, 3 };
List<int> list = array.ToList();
Console.WriteLine(array[0] == list[0]);
Console.WriteLine(array.Length == list.Count);
}
}
Output
True
TrueSome of the more complicated conversion methods involve an array type. These articles demonstrate how you can convert various types, such as ArrayList, string, and List, into arrays and back again.
ArrayList to Array
ArrayList to List
Char Array to String
List to Array
String Array to String
String to Byte Array
Byte Array to String
ToCharArray Method, Convert String to ArrayAnother common task is converting to and from the List type in the C# language. These articles demonstrate how you can do this reliably, by providing many different examples.
Dictionary to List List to DataTable List to StringMost numeric conversions involve a multiplication or division expression. They can be tested using the Google search engine. These examples show how you can convert different units of measurement reliably.
Bytes to Megabytes
Days to Months
Celcius to Fahrenheit
Feet to Inches
Gigabytes to Megabytes
Miles to Kilometers
Milliseconds to Seconds
Nanoseconds to MillisecondsBools. You cannot convert bools and ints directly in the C# programming language. Instead, you must use expressions to convert them as needed.
Bool to Int Int to Bool
These tutorials provide examples for string-specific conversions. There are other string conversions available in this category as well.
Char to String Dictionary to String String to IntThe BitConverter type in the C# language provides a way to convert byte arrays into integral value types, and the opposite. This tutorial helps you discover and use the BitConverter type.
BitConverter
Value types, such as the TimeSpan struct, can be converted into long values. Additionally, the Convert type also provides ways to convert values to Int32 with Convert.ToInt32.
Convert TimeSpan to Long Convert.ToInt32You can define conversion operators in the C# programming language. These work at the level of the language to convert one type to another. Implicit conversions require no special syntax. Explicit conversions require a cast expression.
implicit explicit
For some types and collections, custom conversion methods as shown above are necessary. For simpler value types, using a casting expression is sufficient and will be much faster as well. Please refer to the Cast category on this site for more information on casting expressions.