- 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 to declare and initialize a list in C#?
To declare and initialize a list in C#, firstly declare the list −
List<string> myList = new List<string>()
Now add elements −
List<string> myList = new List<string>() { "one", "two", "three", };
Through this, we added six elements above.
The following is the complete code to declare and initialize a list in C# −
Example
using System; using System.Collections.Generic; class Program { static void Main() { // Initializing collections List<string> myList = new List<string>() { "one", "two", "three", "four", "five", "size" }; Console.WriteLine(myList.Count); } }
Output
6
Advertisements