- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you convert a list collection into an array in C#?
Firstly, set a list collection −
List < string > myList = new List < string > (); myList.Add("RedHat"); myList.Add("Ubuntu");
Now, use ToArray() to convert the list to an array −
string[] str = myList.ToArray();
The following is the complete code −
Example
using System; using System.Collections.Generic; public class Program { public static void Main() { List < string > myList = new List < string > (); myList.Add("RedHat"); myList.Add("Ubuntu"); Console.WriteLine("List..."); foreach(string value in myList) { Console.WriteLine(value); } Console.WriteLine("Converting to Array..."); string[] str = myList.ToArray(); foreach(string s in myList) { Console.WriteLine(s); } } }
Output
List... RedHat Ubuntu Converting to Array... RedHat Ubuntu
Advertisements