List.Add() method with example in C#

C# List.Add() technique: Here, we will find out about the Add() strategy for List with example.

C# List.Add() Method

List.Add() technique is utilized to include the article/component toward the finish of the rundown.

Syntax:

    void List<T>.Add(T item);

Parameter: It acknowledges single a thing of T type to include the List.

Return value: It returns nothing – it’s arrival type is void

Example:

    int list declaration:
    List<int> a = new List<int>();

    adding elements:
    a.Add(10);
    a.Add(20);
    a.Add(30);
    a.Add(40);
    a.Add(50);

    Output:
    10 20 30 40 50

C# Example to add things to the rundown utilizing List.Add() Method:

using System;
using System.Text;
using System.Collections.Generic;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //integer list
            List<int> a = new List<int>();

            //adding elements
            a.Add(10);
            a.Add(20);
            a.Add(30);
            a.Add(40);
            a.Add(50);

            //printing elements
            Console.WriteLine("list elements are...");
            foreach(int item in a)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();

            //string list
            List<string> b = new List<string>();

            //adding elements
            b.Add("Manju");
            b.Add("Amit");
            b.Add("Abhi");
            b.Add("Radib");
            b.Add("Prem");

            //printing elements
            Console.WriteLine("list elements are...");
            foreach (string item in b)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output:

list elements are...
10 20 30 40 50
list elements are...
Manju Amit Abhi Radib Prem

Leave a Comment

error: Alert: Content is protected!!