Add object to Generic List in C#

Hi Friends,

In the below post we will learn about the method used to add elements to the generic list of C#. We have two methods as below.

1. Add(T)

This method takes input of the specified type and append the element to the list. The input can be null for the reference types and the List allows duplicate values. Below is a sample program for it.

using System;
using System.Collections.Generic;

namespace GenericListSample
{
    class Program
    {
        static void Main(string[] args)
        {
            List marksList = new List();
            // add 4 elements
            marksList.Add(12);
            marksList.Add(12);
            marksList.Add(14);
            marksList.Add(18);
            marksList.Add(15);
            //display on console
            foreach (var item in marksList)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

In the above code I have created a generic list ‘marksList ‘ and using ‘Add’ method to append element to that list.

2. AddRange(Ienumerable<T>)

This method takes a collection as input to append to the list. The collection should be of type ‘System.Collections.Generic.IEnumerable<T>’. The input cannot be null but contains null if the specified type is reference type.Below is a sample program for it.

using System;
using System.Collections.Generic;
namespace GenericListSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                List marksList = new List();
                marksList.Add(12); // add one element
                int[] marks = { 12, 14, 18, 15 };
                marksList.AddRange(marks); // add marks collection using AddRange method
                foreach (var item in marksList)
                {
                    Console.WriteLine(item);//display on console
                }
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

Hope this helps you. πŸ™‚

Advertisements

4 thoughts on “Add object to Generic List in C#

  1. Heya i am for the first time here. I came across this board and
    I find It really useful & it helped me out much. I hope to give something back and aid
    others like you helped me.

    Like

  2. First is to thank you for all this informative posts you give us for free; i bet all of us are happy.
    Such a great idea of yours! You have been a big help for me. Thanks a lot. more post for interesting topic. Great!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s