C# | List announcement with instatement: Here, we will figure out how to make/proclaim a rundown with the introduction?
The assignment is to make/proclaim a rundown with an initializer list in C#.
C# List
A rundown is utilized to speak to the rundown of the items, it is spoken to as List, where T is the kind of the rundown objects/components.
A rundown is a class which goes under System.Collections.Generic bundle, so we need to incorporate it first.
Rundown assertion with instatement
To pronounce and instate a rundown with the components/objects, we utilize the accompanying syntax,
List<T> list_name= new List<T> {list_of_objects/elements};
Here, T is the sort and list_name is the name of the rundown.
Examples:
//an integer list
List<int> int_list = new List<int> { 10, 20, 30, 40, 50 };
//a string list
List<string> str_list = new List<string>{
"Manju", "Amit", "Abhi", "Radib", "Prem"
};
Here, int_list is a rundown of integer components and str_list is a rundown of string components.
C# program to make/announce a rundown with instatement:
using System;
using System.Text;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void Main(string[] args)
{
//an integer list
List<int> int_list = new List<int> { 10, 20, 30, 40, 50 };
//a string list
List<string> str_list = new List<string>{
"Manju", "Amit", "Abhi", "Radib", "Prem"
};
//printing list elements
Console.WriteLine("int_list elements...");
foreach (int item in int_list)
{
Console.Write(item + " ");
}
Console.WriteLine();
Console.WriteLine("str_list elements...");
foreach (string item in str_list)
{
Console.Write(item + " ");
}
Console.WriteLine();
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output:
int_list elements...
10 20 30 40 50
str_list elements...
Manju Amit Abhi Radib Prem