C# collection Part-3(Queue-HashTable)
What is a Queue in C#?
The Queue is a special case collection which represents a first in first out concept.
Declaration of the Queue
The declaration of a Queue is provided below. A Queue is created with the help of the Queue Datatype. The "new" keyword is used to create an object of a Queue. The object is then assigned to the variable qt.
Queue qu = new Queue();
Adding elements to the Queue
The enqueue method is used to add an element into the queue. The general syntax of the statement is given below.
Queue.enque(element);
Count
This property is used to get the number of items in the queue. Below is the general syntax of this statement.
Queue.count
Contains
This method is used to see if an element is present in the Queue. Below is the general syntax of this statement. The statement will return true if the element exists, else it will return the value false.
Queue.contains(element)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Queue qu = new Queue();
qu.Enqueue(1);
qu.Enqueue(2);
qu.Enqueue(3);
foreach (Object obj in qu)
{
Console.WriteLine(obj);
}
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("The number of elements in the Queue " + qt.Count);
Console.WriteLine("Dose the Queue contain " + qt.Contains(3));
Console.ReadKey();
}
}
}
C# Queue Dequeue
Now let's look at the remove functionality. We will see the code required to remove the last element from the queue.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Queue qt = new Queue();
qt.Enqueue(1);
qt.Enqueue(2);
qt.Enqueue(3);
qt.Dequeue();
foreach (Object obj in qt)
{
Console.WriteLine(obj);
}
Console.ReadKey();
}
}
}
What is Hashtable in C#?
A hash table is a special collection that is used to store key-value items. So instead of storing just one value like the stack, array list and queue, the hash table stores 2 values. These 2 values form an element of the hash table.
Declaration of the Hashtable
Hashtable hs = new Hashtable();
Adding elements to the Hashtable
The Add method is used to add an element on to the queue. The general syntax of the statement is given below
hs.add("key","value");
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add("001",".Net");
ht.Add("002","C#");
ht.Add("003","ASP.Net");
ICollection keys = ht.Keys;
foreach (String k in keys)
{
Console.WriteLine(ht[k]);
}
Console.ReadKey();
}
}
}
ContainsKey
This method is used to see if a key is present in the Hashtable.Hashtable.ContainsKey(value)ContainsValue
This method is used to see if a key is present in the Hashtable.Hashtable.ContainsValue(Key)using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DemoApplication { class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add("001",".Net"); ht.Add("002","C#"); ht.Add("003","ASP.Net"); Console.WriteLine(ht.ContainsKey("001")); Console.WriteLine(ht.ContainsValue("C#")); Console.ReadKey(); } } }
Comments
Post a Comment