Menu Close

Blog

Slide 1

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

Slide 2

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

Slide 3

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

previous arrow
next arrow

LIST COLLECTION CLASS IN C#

List Collection class in C#

List is one of the generic collection classes present in system.collection.generic namespace.

There are several generic collection classes in system.collection.generic namespace as listed below.

1. Dictionary 
2.List
3.Stack
4.Queue etc

A List class can be used to create a collection of any type.

For example, we can create a list of integers,strings  and even complex types.

The objects stored in the list can be accessed by index.

This class also provides methods to search, sort, and manipulate lists.



using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            Customer Customer1 = new Customer()
            {
                ID = 101,
                Name = “Malla”,
                Salary = 5000
            };
            Customer Customer2 = new Customer()
            {
                ID = 102,
                Name = “Mark”,
                Salary = 5000
            };

            Customer Customer3 = new Customer()
            {
                ID = 119,
                Name = “Mla”,
                Salary = 5000
            };

            List<Customer> customers = new List<Customer>(2);
            customers.Add(Customer1);
            customers.Add(Customer2);
            customers.Add(Customer3);

            for (int i = 0; i < customers.Count; i++)
            {
             Customer c = customers[i];
             Console.WriteLine(“ID = {0}, Name = {1},Salary = {2}”, c.ID, c.Name, c.Salary);
             Console.ReadLine(); 
            }

            //foreach (Customer c in customers)
            //{
            // Console.WriteLine(“ID = {0}, Name = {1},Salary = {2}”, c.ID, c.Name, c.Salary);
            // Console.ReadLine(); 
            //}
            
          
           
        }

      public class Customer
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public int Salary { get; set; }
      }
    }

}


   =====================================================================

 customers.Insert(0, Customer3);

            foreach(Customer c in customers)
            {
                Console.WriteLine(c.ID);
                Console.ReadLine();
            }

 ====================================================================

using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            Customer Customer1 = new Customer()
            {
                ID = 101,
                Name = “Malla”,
                Salary = 5000
            };
            Customer Customer2 = new Customer()
            {
                ID = 102,
                Name = “Mark”,
                Salary = 5000
            };

            Customer Customer3 = new Customer()
            {
                ID = 119,
                Name = “Mla”,
                Salary = 5000
            };

            List<Customer> customers = new List<Customer>(2);
            customers.Add(Customer1);
            customers.Add(Customer2);
            customers.Add(Customer3);
            customers.Insert(0, Customer3);

            Console.WriteLine(customers.IndexOf(Customer3,1,3));
            Console.ReadLine();
        }

      public class Customer
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public int Salary { get; set; }
      }
    }

}
=======================================================================
1. Contains() function- Checks if an item exists in the list.This method returns true if the item exists, else false.


2.Exists() function-Checks if an item exists in the list based on a condition. This method returns    true if the items exists, else false.

3.Find() function – Searches for an element that matches the conditions defined by the specified    lambda expression and returns the first matching item from the list.

4. FindLast() function-Searches for an element that matches the conditions defined by the         specified lambda expression and returns the last matching item from the list.


5. FindAll() function-Returns all the items from the list that match the conditions specified by the lambda expression.



6.FindIndex() function- Returns the index of the first item, that matches the condition specified by the lambda expression. There are 2 other overloads of this method which allows us to specify the range of elements to search, with in the list.

7.FindLastIndex() function-Returns the index of the last item, that matches the condition specified by the lambda expression. There are 2 other overloads of this method which allows us to specify the range of elements to search, with in the list.

Convert an array to a List – Use ToList() method

Convert a list to an array – Use ToArray() method


Convert a List to a Dictionary – Use ToDictionary() method


using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            Customer Customer1 = new Customer()
            {
                ID = 101,
                Name = “Malla”,
                Salary = 5000
            };
            Customer Customer2 = new Customer()
            {
                ID = 102,
                Name = “Mark”,
                Salary = 5000
            };

            Customer Customer3 = new Customer()
            {
                ID = 119,
                Name = “Mla”,
                Salary = 5000
            };

            List<Customer> listcustomers = new List<Customer>(2);
            listcustomers.Add(Customer1);
            listcustomers.Add(Customer2);
           
            if(listcustomers.Contains(Customer3))
            {
                Console.WriteLine(“Customers3 object exists in the lsit”);
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine(“Customer3 object does not exist in the list”);
                Console.ReadLine();          
            }
            
        }

      public class Customer
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public int Salary { get; set; }
      }
    }

}


   

   =====================================================================
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            Customer Customer1 = new Customer()
            {
                ID = 101,
                Name = “Malla”,
                Salary = 5000
            };
            Customer Customer2 = new Customer()
            {
                ID = 102,
                Name = “Mark”,
                Salary = 5000
            };

            Customer Customer3 = new Customer()
            {
                ID = 119,
                Name = “Mla”,
                Salary = 5000
            };

            List<Customer> listcustomers = new List<Customer>(2);
            listcustomers.Add(Customer1);
            listcustomers.Add(Customer2);
            listcustomers.Add(Customer3);
           
            if(listcustomers.Exists(cust => cust.Name.StartsWith(“Z”)))
            {
                Console.WriteLine(“Customers3 object exists in the list”);
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine(“Customer3 object does not exist in the list”);
                Console.ReadLine();          
            }
        }

      public class Customer
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public int Salary { get; set; }
      }
    }

}


   ———————————————————————————————————————
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            Customer Customer1 = new Customer()
            {
                ID = 101,
                Name = “Malla”,
                Salary = 5000
            };
            Customer Customer2 = new Customer()
            {
                ID = 102,
                Name = “Mark”,
                Salary = 7000
            };

            Customer Customer3 = new Customer()
            {
                ID = 119,
                Name = “Mla”,
                Salary = 5500
            };

            List<Customer> listcustomers = new List<Customer>(2);
            listcustomers.Add(Customer1);
            listcustomers.Add(Customer2);
            listcustomers.Add(Customer3);

            Customer c = listcustomers.Find(cust => cust.Salary > 5000);
           //  Customer c = listcustomers.FindLast(cust => cust.Salary > 5000);
            Console.WriteLine(“ID={0},Name = {1},Salary = {2}”, c.ID, c.Name, c.Salary);
            Console.ReadLine();
  
        }

      public class Customer
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public int Salary { get; set; }
      }
    }

}


   =======================================================================
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            Customer Customer1 = new Customer()
            {
                ID = 101,
                Name = “Malla”,
                Salary = 5000
            };
            Customer Customer2 = new Customer()
            {
                ID = 102,
                Name = “Mark”,
                Salary = 7000
            };

            Customer Customer3 = new Customer()
            {
                ID = 119,
                Name = “Mla”,
                Salary = 5500
            };

            List<Customer> listcustomers = new List<Customer>(2);
            listcustomers.Add(Customer1);
            listcustomers.Add(Customer2);
            listcustomers.Add(Customer3);

            List<Customer> customers = listcustomers.FindAll(cust => cust.Salary > 5000);
            foreach (Customer c in customers)
            {
                Console.WriteLine(“ID={0},Name = {1},Salary = {2}”, c.ID, c.Name, c.Salary);
                Console.ReadLine();
            }
       
        }

      public class Customer
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public int Salary { get; set; }
      }
    }

}


   

Share this:

DICTIONARY IN C#

1.A dictionary is a collection of(Key.value)pairs
2.Dictionary class is present in System.Collection.Generic namespace.
3.When creating s dictionary , we need to specify the type for key and value.
4.Dictionary provides fast lookups for values using keys.
5.Keys in the dictionary must be unique.


using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            Customer Customer1 = new Customer()
            {
                ID = 101,
                Name = “Malla”,
                Salary = 5000
            };
            Customer Customer2 = new Customer()
            {
                ID = 102,
                Name = “Mark”,
                Salary = 5000
            };

            Customer Customer3 = new Customer()
            {
                ID = 119,
                Name = “Mla”,
                Salary = 5000
            };

            Dictionary<int, Customer> dictionaryCustomer = new Dictionary<int, Customer>();
            dictionaryCustomer.Add(Customer1.ID, Customer1);
            dictionaryCustomer.Add(Customer2.ID, Customer2);
            dictionaryCustomer.Add(Customer3.ID, Customer3);

            Customer Customer119 = dictionaryCustomer[119];
          foreach(KeyValuePair<int,Customer> customerkeyValuePair in dictionaryCustomer)
          {
              Console.WriteLine(“Key = {0}”, customerkeyValuePair.Key);
              Customer cust = customerkeyValuePair.Value;
              Console.WriteLine(“ID ={0}, Name ={1},Salary = {2}”, cust.ID, cust.Name, cust.Salary);
              Console.WriteLine(“———————————–“);
              Console.ReadLine();

          }
            Console.ReadLine();
        }

      public class Customer
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public int Salary { get; set; }
      }
    }

}
=================================================================
Methods of dictionary class
1. TryGetValue()
2.Count()
3.Remove()
4.Clear()
5.Using LINQ extension methods with Dictionary

6.Different ways to convert an array into a dictionary.


using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            Customer Customer1 = new Customer()
            {
                ID = 101,
                Name = “Malla”,
                Salary = 5000
            };
            Customer Customer2 = new Customer()
            {
                ID = 102,
                Name = “Mark”,
                Salary = 5000
            };

            Customer Customer3 = new Customer()
            {
                ID = 119,
                Name = “Mla”,
                Salary = 5000
            };

            Dictionary<int, Customer> dictionaryCustomer = new Dictionary<int, Customer>();
            dictionaryCustomer.Add(Customer1.ID, Customer1);
            dictionaryCustomer.Add(Customer2.ID, Customer2);
            dictionaryCustomer.Add(Customer3.ID, Customer3);

            Console.WriteLine(“Total Items = {0}”, dictionaryCustomer.Count);
            Console.ReadLine();

            dictionaryCustomer.Remove(110);
            Console.ReadLine();
            //Customer cust;
            //if (dictionaryCustomer.TryGetValue(101, out cust))
            //{
            //    Console.WriteLine(“ID = {0}, Name = {1}, Salary = {2}”, cust.ID, cust.Name, cust.Salary);
            //    Console.ReadLine();
            //}
            //else
            //{
            //    Console.WriteLine(“The Key is not found”);
            //}
        
           
        }

      public class Customer
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public int Salary { get; set; }
      }
    }

}

   

Share this:

CODE SNIPPETS IN VISUAL STUDIO

CODE SNIPPET TYPES

Expansion : These snippets allows the code snippet to be inserted at the cursor.

SurroundsWith: These snippets allows the code snippets to be placed around a selected piece of code

Refactoring: These snippets are used during code refactoring.

Control K X for intellesense in visual studio

right click in visual studio program and select the insert snippets > visual c# > for loop

CODE SNIPPET MANAGER

Tool > code snippet manager

Share this:

HOW TO MAKE METHOD PARAMETERS OPTIONAL IN C#

OPTIONAL PARAMETERS

FOUR WAYS

1) Use parameter arrays
2) Method overloading
3) Specify parameter defaults
4) Use OptionalAttribute that is present in System.Runtime.InteropService namespace


1)Use parameter arrays


using System;
using System.Text;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            AddNumber(10, 20 new object[] {30,50,40});
        }

        public static void AddNumber(int firstNumber, int SecondNumber, params object[] restofNumbers)
        {
            int result = firstNumber + SecondNumber;
            if(restofNumbers != null)
            {
                foreach(int i in restofNumbers)
                {
                    result += i;
                }
            }
            Console.WriteLine(“Sum = ” + result);
            Console.ReadLine();
        }

    }

}


   

2) Making method parameters optional using Method overloading 


using System;
using System.Text;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            AddNumber(10, 20, new int[] {30,40});
        }
// over loading method
      public static void AddNumber(int firstNumber, int SecondNumber)
        {
            AddNumber(firstNumber, SecondNumber, null);
        }
       

        public static void AddNumber(int firstNumber, int SecondNumber, params int[] restofNumbers)
        {
            int result = firstNumber + SecondNumber;
            if(restofNumbers != null)
            {
                foreach(int i in restofNumbers)
                {
                    result += i;
                }
            }
            Console.WriteLine(“Sum = ” + result);
            Console.ReadLine();
        }

    }

}

3) Specify parameter defaults


using System;
using System.Text;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            AddNumber(10, 20, new int[] {30,40});
        }

        // optional parameters appears after all the parameters
     
        public static void AddNumber(int firstNumber, int SecondNumber,  int[] restofNumbers = null)
        {
            int result = firstNumber + SecondNumber;
            if(restofNumbers != null)
            {
                foreach(int i in restofNumbers)
                {
                    result += i;
                }
            }
            Console.WriteLine(“Sum = ” + result);
            Console.ReadLine();
        }

    }

}


   NAMED PARAMETERS

using System;
using System.Text;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            Test(1,c:20);
        }

        // optional parameters appears after all the parameters

        public static void Test(int a, int b = 10, int c = 20)
        {
            Console.WriteLine(“a = ” + a);
            Console.WriteLine(“b = ” + b);
            Console.WriteLine(“c = ” + c);
            Console.ReadLine();
        }

    }

}

 4)  Making Method parameters optional by using OptionalAttributes


using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            AddNumber(10, 20);
        }
        
        public static void AddNumber(int firstNumber, int SecondNumber,[Optional] int[] restofNumbers)
        {
            int result = firstNumber + SecondNumber;
            if (restofNumbers != null)
            {
                foreach (int i in restofNumbers)
                {
                    result += i;
                }
            }
            Console.WriteLine(“Sum = ” + result);
            Console.ReadLine();
        }
    }
}
   


Share this: