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;
- 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();
}
{
// 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<int, string> valuePairs = new Dictionary<int, string>();
//INSERT
valuePairs.Add(1, "Nirmal");
valuePairs.Add(2, "Sheeba");
valuePairs.Add(3, "xxx");
Console.WriteLine("INSERT");
foreach (KeyValuePair<int, string> valuePair in valuePairs)
{
Console.WriteLine("Result : Key -> " + valuePair.Key + " Value -> " + valuePair.Value);
}
Console.WriteLine("\n");
//UPDATE
valuePairs[3] = "Sandeep";
Console.WriteLine("UPDATE");
foreach (KeyValuePair<int, string> 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<int, string> 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<int, string> valuePairs = new SortedList<int, string>();
//INSERT
valuePairs.Add(1, "Nirmal");
valuePairs.Add(2, "Sheeba");
valuePairs.Add(3, "xxx");
Console.WriteLine("INSERT");
foreach (KeyValuePair<int, string> valuePair in valuePairs)
{
Console.WriteLine("Result : Key -> " + valuePair.Key + " Value -> " + valuePair.Value);
}
Console.WriteLine("\n");
//UPDATE
valuePairs[3] = "Sandeep";
Console.WriteLine("UPDATE");
foreach (KeyValuePair<int, string> 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<int, string> 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
Post a Comment