My first experience with LISTS in C#

Csharp
Csharp

My first experience with LISTS in C#

Welcome to CodeSpread!

Hi guys, this is Sathish, Lets discuss the concepts of lists.

These are the concepts which I have planned to discuss with you guys today.

  1. What is a List and how it is advantageous than array list
  2. Creation of the list.
  3. Different methods in list.

What is a list?

A List is a collection of items that can be accessed by index.Lists can re-size dynamically where as Arrays have a limited size which is declared when they are initialized. List, as an advantage,do not need to be declared or managed during initialization.

List class always resides in System.Collections.Generic namespace.

Creation of List :

The List class constructor takes a key data type. The data type can be
any .NET data type.

Consider these example for creating a list as integer,double and string
respectively.


ListListDemo = new List();

ListListDemo = new List();

ListListDemo = new List();

These is how we can create a List.

Just a reminder, In case of key,You need to replace the datatype which your going to use.

List provides different methods like:

Add,remove,clear,contains,capacity,count distinct,max,min,sort,reverse
insert and many other methods are there.

Lets discuss about this in today’s topic.

Adding Elements to the list.

ListListDemo = new List();
ListDemo.Add("sathish");
ListDemo.Add("Kumar");
ListDemo.Add("sathish");
ListDemo.Add("Kumar");
foreach (string lines in ListDemo)
{
Console.WriteLine(lines);

}
Console.ReadKey(true);

In the above example I have added 2 elements to a list having string data type.

Remove Elements in list :

ListListDemo = new List();
ListDemo.Add("Sathish");
ListDemo.Add("Kumar");
ListDemo.Add("Mahesh");
ListDemo.Remove("Kumar");
foreach (string lines in ListDemo)
{
Console.WriteLine(lines);
}
Console.ReadKey(true);

In the above example, it searches the string list and remove the element according to key specified.

Sorting Elements in the list.

Sometimes it is required to sort the list in ascending or descending order.List provides one more method called sort(),where you can sort the list.


List<string> ListDemo = new List<string>();
ListDemo.Add("Sathish");
ListDemo.Add("Kumar");
ListDemo.Add("Mahesh");
ListDemo.Remove("Kumar");
foreach (string lines in ListDemo)
{
Console.WriteLine(lines);
}
Console.ReadKey(true);

Inserting an element to a list at specified position.

Where it inserts the string to the 4th position.


List<string> ListDemo = new List<string>();
ListDemo.Add("Sathish");
ListDemo.Add("Kumar");
ListDemo.Add("Mahesh");
ListDemo.Insert(3, "Kumar");
foreach (string lines in ListDemo)
{
Console.WriteLine(lines);
}
Console.ReadKey(true);

Finding the  element in the list :

List<string> ListDemo = new List<string>();
ListDemo.Add("Sathish");
ListDemo.Add("Kumar");

ListDemo.Add("Mahesh");
ListDemo.Insert(3, "Kumar");
if (ListDemo.Contains("Sathish"))
{
Console.WriteLine("The Elemets already Contains in the list");

}
else
{

Console.WriteLine("The Elements is not there in the list");

}

This is how we can determine whether list contains any duplication in a list.

Removing the Duplication elements in the list :

List Provides one more method called as distinct that removes the duplicated elements directly.


List<string> ListDemo = new List<string>();
ListDemo.Add("Sathish");
ListDemo.Add("Kumar");
ListDemo.Add("Mahesh");
ListDemo.Insert(3, "Kumar");
List Chck = ListDemo.Distinct().ToList();
Console.WriteLine(Chck.Count);
foreach (string lines in Chck)
{
Console.WriteLine(lines);
}

Console.ReadKey(true);

If we execute this program ,It eliminates ‘Kumar’,because it is repeated twice.where count() function returns number of elements present in the list.

Finding maximum,minimum and reversing the list :

Reversing the List :

ListListdemo = new List();
Listdemo.Add(1);
Listdemo.Add(2);
Listdemo.Add(3);
Listdemo.Reverse();
foreach (int lines in Listdemo)
{
Console.WriteLine(lines);
}

Console.ReadKey(true);

Maximum and Minimum values in the List :

List<int> Listdemo = new List<int>();
Listdemo.Add(1);
Listdemo.Add(2);
Listdemo.Add(3);

Console.WriteLine("The Maximum Value in the list is : {0}",Listdemo.Max());
Console.WriteLine("The Minimum Value in the list is : {0}", Listdemo.Min());
foreach (int lines in Listdemo)
{
Console.WriteLine(lines);
}

Console.ReadKey(true);

I hope this article makes you more comfortable with the concepts of lists.

Please post your valuable comments here or mail it to admin@codespread.com. We need your feedback to add value to our articles.

Author: Sathish

I just loved to express myself...

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • C# – Box(ing) and Un-box(ing)C# – Box(ing) and Un-box(ing)I have heard this concept from many people and their different versions so I always wanted to share some insight on it which can work as a one stop solution. What is boxing and un-boxing? The Con...
  • Indexers,A Short Note.Indexers,A Short Note.What is an indexer? Indexers can be easily remembered as arrays. These arrays are special as they allow the class to be treated as arrays and access the data stored within the object. For example:...
  • Interview Question: Why Garbage Collection required?Interview Question: Why Garbage Collection required?What is garbage collection? In two words “Memory Management”. In two lines “Managing the lifetime of object starting from creation, allocation and release it back to free memory pool.” Why it ...
  • 20 Dec: Must Read Codes [6-10]20 Dec: Must Read Codes [6-10]We are sharing few must read codes/concepts which are required now and then. Each of these codereads doesn’t require a full length descriptive article so combining them into groups of five. CodeRe...
  • Asp.Net: More about CookiesAsp.Net: More about CookiesWe have seen, how our cookies looks in our last article Cookies Part 1. In this article, we will talk about their properties,limitations and technical part. Properties and Limitations Cook...
  • Useful ‘ref’ and ‘out’ parametersUseful ‘ref’ and ‘out’ parameters It took me some time to realize the real potential of ref and out keywords, till the time I experience a condition which could only be simplified by implementing ref and out keyword. Lets st...
  • Nullable .Net typesNullable .Net typesWe declare .net types in our project daily but still we do not recollect default values for each .net type so we end up doing null check for each data type. Its a basic mistake of a developer but ....
  • C#: Useful JSON in .NetC#: Useful JSON in .NetWhy JSON? All web applications revolve around large chunk of data and availability of that data to our application is of utmost importance. To do that, we have options like fetch data from databas...