- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
- Related Articles
- How to declare and initialize a dictionary in C#?
- How to declare and initialize constant strings in C#?
- How do you declare, initialize and access jagged arrays in C#?
- How to initialize a list to an empty list in C#?
- How to declare, create, initialize and access an array in Java?
- What is a string? Declare and initialize the strings in C language
- How do I declare and initialize an array in Java?
- How to easily initialize a list of Tuples in C#?
- How to initialize List in Kotlin?
- Java Program to Initialize a List
- How to initialize a vector in C++?
- How to Initialize and Compare Strings in C#?
- How to initialize an empty array list in Kotlin?
- How to declare a delegate in C#?
- How to declare a tuple in C#?

Advertisements