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

SORT A LIST OF SIMPLE TYPES IN C#

using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {

            List<int> numbers = new List<int>() { 1, 9, 12, 8, 6, 3 };
            Console.WriteLine(“Numbers before sorting”);
            foreach(int number in numbers)
            {
                Console.WriteLine(number);
                Console.ReadLine();
            }
            numbers.Sort();

           
            Console.WriteLine(“Numbers after sorting”);
            foreach (int number in numbers)
            {
                Console.WriteLine(number);
                Console.ReadLine();
            }

            numbers.Reverse();
            Console.WriteLine(“Numbers in descending order”);
            foreach (int number in numbers)
            {
                Console.WriteLine(number);
                Console.ReadLine();
            }

            List<string> alphabets = new List<string>() { “B”, “F”, “D”, “E”, “A”, “C” };
            Console.WriteLine(“Alphabets before sorting”);
            foreach (string alphabet in alphabets)
            {
                Console.WriteLine(alphabet);
                Console.ReadLine();
            }
            alphabets.Sort();

            Console.WriteLine(“Alphabets after sorting”);
            foreach (string alphabet in alphabets)
            {
                Console.WriteLine(alphabet);
                Console.ReadLine();
            }

            alphabets.Reverse();

            Console.WriteLine(“Alphabets in descending order “);
            foreach (string alphabet in alphabets)
            {
                Console.WriteLine(alphabet);
                Console.ReadLine();
            }
            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
            };
        }

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

   
Share this:

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: