Ticker

20/recent/ticker-posts

Advertisement

Implement C# list - Algomentor


C# list - A complete Guide


C# list

What is a list in C#?

List in C# is a collection of integer, string or any object list is one of the most powerful things to learn in a C# because without a list you can't store data in a structural way. there are some other tutorials in which I demonstrate how a linked list works. But creating a linked list and using it is not a solution because C# provide you the List<T> class that will make your job easy. you just need to import it and start using it no need to maintain complex code and this will also help you in competitive coding.

How can we create a list in C#?

Creating a list is just a one-line code in C Sharpe I will show up by creating a list of integers, strings, and custom classes.

  • List<int> list = new List<int> 
  • List<string> list = new List<string> 
  • List<MyClass> list = new List<MyClass> 

What are the methods available in the C# list?

There are so many methods prebuilt in C sharp list common method are Add(), Remove(), and Find() there are also many methods associative with the list in C# by one program I will demonstrate all the functionality that we have in the C# list.

A Demo Program to demonstrate a list of integer in C#

This program will demonstrate the working of some list methods in C# like Add, Remove, Clear, Count.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algomentor
{
    class Program
    {
        public static void printList(List<int> list)
        {
            foreach(var item in list)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine("");
        }
        static void Main(string[] args)
        {
            // creating a list
            List<int> list = new List<int>();

            // insert some integer into the list
            list.Add(10);
            list.Add(20);
            list.Add(30);
            list.Add(40);
            list.Add(50);
            list.Add(60);
            list.Add(70);
            list.Add(80);

            //printing the list
            printList(list);

            // remove some integer from list
            list.RemoveAt(1); // here 1 is the index and index start with 0 so it will remove 20

            //printing the list
            printList(list);

            //remove by value
            list.Remove(80); // it will first find 80 and if it finds 80 in the list it will remove it

            //printing the list
            printList(list);

            list.RemoveRange(1, 3); // so it will start removing from index 1 and then delete 3 element in the list

            //printing the list
            printList(list);

            //this will give the count of element in the list
            int count = list.Count(); // this will count the element in the list and assign to count variable
            Console.WriteLine(count);

            //this function will reverse the list
            list.Reverse();

            //printing the list
            printList(list);

            //this will give average of the list
            int li = (int)list.Average(); // this average function return the average of the list 
            Console.WriteLine(li);

            //create another list
            List<int> newlist = new List<int>();
            newlist.Add(100);
            newlist.Add(200);
            newlist.Add(300);

            //finding maximum in the list
            int max = newlist.Max();
            Console.WriteLine(max);

            //finding minimum in the list
            int min = newlist.Min();
            Console.WriteLine(min);

            // this will find element in the list using binary search If the list is sorted 
            //and it find the element it will return 1 otherwise 0 
            int v = newlist.BinarySearch(200); 
            Console.WriteLine(v);

            // this will clear the complete list.
            list.Clear();
            newlist.Clear();
        }
    }
}

You can the namespace according to you in my case it's algomentor but for you. it may be practice or XYZ

How to create a custom function for the C# list?

I will demonstrate this question by the code for that I am going to create a find function and its job is to find whether the element is in the list or not if it was there it will return 1 otherwise 0.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algomentor
{
    class Program
    { 
        // custom find is the function that we are going to create to find element in our list
        public static int CustomFind(List<int> list, int value)
        {
            foreach(var i in list)
            {
                if(i==value)
                {
                    return 1;
                }
            }
            return 0;
        }
        static void Main(string[] args)
        {
            // creating a list
            List<int> list = new List<int>();

            // insert some integer into the list
            list.Add(10);
            list.Add(20);
            list.Add(30);
            list.Add(40);
            list.Add(50);
            list.Add(60);
            list.Add(70);
            list.Add(80);

            // so now we call the CustomFind function

            // this will return code 1 because 20 is in the list
            int code = CustomFind(list,20);
            Console.WriteLine(code);

            // this will return 0 because 100 is not in the list
            int code2 = CustomFind(list, 100);
            Console.WriteLine(code2);
        }
    }
}

Here are some useful link you that will help to learn More

So with that,, I will conclude the lesson in some other blog you will find some advanced tutorial of C#, java, and about C# list if you have any trouble then please let us know in the comment section. 

Thankyou so much guys for completely reading the blog.

Post a Comment

0 Comments