Skip to main content

How to perform Insert/Update/Delete operation in Generic Collections in C#

Getting Started

In ASP.NET 5 we can create console applications. To create a new console application, we first open Visual Studio 2015. Create it using File-> New-> Project.


Now we will select the ASP.NET 5 Console Application because we will create the console application and click on the OK button.


we need to include the following references in our application.
  1. using System;
  2. using System.Collections.Generic;
  1. Insert, Update, Delete, Get perform in List collection.
public static void DML_List()
{
// Creating a List of integers
IList<int> list = new List<int>();
// Printing the insert opeartion
Console.WriteLine("INSERT");
// Adding elements in list collection
list.Add(100);
list.Add(200);
list.Add(300);
list.Add(400);
list.Add(400);
// Printing the element from list collection
foreach (object x in list)
{
Console.WriteLine("Result : " + x);
}
Console.WriteLine("\n");
// Printing the update opeartion
Console.WriteLine("UPDATE");
// Updating new value in particular element in the list
list[4] = 500;
// Printing the element from list collection
foreach (object x in list)
{
Console.WriteLine("Result : " + x);
}
Console.WriteLine("\n");
// Printing the get opeartion
Console.WriteLine("GET BY KEY");
//Pass the value from list collection
int Value = list[3];
// Printing the element from integer
Console.WriteLine(Value);
Console.WriteLine("\n");
// Printing the delete opeartion
Console.WriteLine("DELETE");
//Remove the particular element from list
list.RemoveAt(2);
// Printing the element from list collection
foreach (object x in list)
{
Console.WriteLine("Result : " + x);
}
Console.WriteLine("\n");
//Remove/Refresh all element from list
list.Clear();

}
The output will be:
 
Insert, Update, Delete, Get perform in Dictionary.
public static void DML_Dictionary()
{
Dictionary<intstring> valuePairs = new Dictionary<intstring>();
//INSERT
valuePairs.Add(1, "Nirmal");
valuePairs.Add(2, "Sheeba");
valuePairs.Add(3, "xxx");
Console.WriteLine("INSERT");
foreach (KeyValuePair<intstring> valuePair in valuePairs)
{
Console.WriteLine("Result : Key -> " + valuePair.Key + " Value -> " + valuePair.Value);
}
Console.WriteLine("\n");
//UPDATE
valuePairs[3] = "Sandeep";
Console.WriteLine("UPDATE");
foreach (KeyValuePair<intstring> valuePair in valuePairs)
{
Console.WriteLine("Result : Key -> " + valuePair.Key + " Value -> " + valuePair.Value);
}
Console.WriteLine("\n");
//GET
string Value = valuePairs[3].ToString();
Console.WriteLine("GET BY KEY");
Console.WriteLine(Value);
Console.WriteLine("\n");
//DELETE
Console.WriteLine("DELETE");
valuePairs.Remove(3);
foreach (KeyValuePair<intstring> valuePair in valuePairs)
{
Console.WriteLine("Result : Key -> " + valuePair.Key + " Value -> " + valuePair.Value);
}
Console.WriteLine("\n");
//DELETE ALL
valuePairs.Clear();
}
The output will be:
 
Insert, Update, Delete, Get perform in SortedList.
public static void DML_SortedList()
{
SortedList<intstring> valuePairs = new SortedList<intstring>();
//INSERT
valuePairs.Add(1, "Nirmal");
valuePairs.Add(2, "Sheeba");
valuePairs.Add(3, "xxx");
Console.WriteLine("INSERT");
foreach (KeyValuePair<intstring> valuePair in valuePairs)
{
Console.WriteLine("Result : Key -> " + valuePair.Key + " Value -> " + valuePair.Value);
}
Console.WriteLine("\n");
//UPDATE
valuePairs[3] = "Sandeep";
Console.WriteLine("UPDATE");
foreach (KeyValuePair<intstring> valuePair in valuePairs)
{
Console.WriteLine("Result : Key -> " + valuePair.Key + " Value -> " + valuePair.Value);
}
Console.WriteLine("\n");
//GET
string Value = valuePairs[3].ToString();
Console.WriteLine("GET BY KEY");
Console.WriteLine(Value);
Console.WriteLine("\n");
//DELETE
Console.WriteLine("DELETE");
valuePairs.Remove(3);
foreach (KeyValuePair<intstring> valuePair in valuePairs)
{
Console.WriteLine("Result : Key -> " + valuePair.Key + " Value -> " + valuePair.Value);
}
Console.WriteLine("\n");
//DELETE ALL
valuePairs.Clear();
}
The output will be:
 
Insert, Delete, Get perform in Stack.
public static void DML_Stack()
{
Stack<string> stk = new Stack<string>();
//INSERT
Console.WriteLine("INSERT");
stk.Push("Family1");
stk.Push("Family2");
stk.Push("Family2");
stk.Push("Family4");
foreach (object x in stk)
{
Console.WriteLine("Result: " + x);
}
Console.WriteLine("\n");
Console.WriteLine("UPDATE OPEARTION HAS NOT PERFORM");
Console.WriteLine("\n");
//GET
string Value = stk.Peek();
Console.WriteLine("GET ONLY TOP ELEMENT");
Console.WriteLine(Value);
Console.WriteLine("\n");
//DELETE
Console.WriteLine("DELETE ONLY TOP ELEMENT");
stk.Pop();
foreach (object x in stk)
{
Console.WriteLine("Result: " + x);
}
Console.WriteLine("\n");
//DELETE ALL
stk.Clear();
}
The output will be:
 
Insert, Delete, Get perform in Queue
public static void DML_Queue()
{
Queue<string> queue = new Queue<string>();
//INSERT
Console.WriteLine("INSERT");
queue.Enqueue("Family1");
queue.Enqueue("Family2");
queue.Enqueue("Family2");
queue.Enqueue("Family4");
foreach (object x in queue)
{
Console.WriteLine("Result: " + x);
}
Console.WriteLine("\n");
Console.WriteLine("UPDATE OPEARTION HAS NOT PERFORM");
Console.WriteLine("\n");
//GET
string Value = queue.Peek();
Console.WriteLine("GET ONLY TOP ELEMENT");
Console.WriteLine(Value);
Console.WriteLine("\n");
//DELETE
Console.WriteLine("DELETE ONLY TOP ELEMENT");
queue.Dequeue();
foreach (object x in queue)
{
Console.WriteLine("Result: " + x);
}
Console.WriteLine("\n");
//DELETE ALL
queue.Clear();
}
The output will be:
 
I hope this article is helpful for the perform DML operation on generic Collections using C#, when they want to begin working in the ASP.NET 5 console applications. Thanks for reading this article.
Happy Coding

Comments

Popular posts from this blog

How to perform lazy loading with generic method.

Introduction Let’s develop C# console application to perform lazy loading with generic method.    Getting Started In ASP.NET 5 we can create console applications. To create a new console application, we first open Visual Studio 2015. Create it using File-> New-> Project. Now we will select the ASP.NET 5 Console Application because we will create the console application and click on the OK button. We need to include the following references in our application. using  System;   using  System.Collections.Generic;   using  System.Linq;  In the main method, I have created 2 classes and 1 method EducationProfile<T> – To define class members. Candiate<T> = Initialize lazy loading in the class. CallLazyLoading = call the function Please refer to the code snippet below static   void  Main( string [] args)   {        try ...