0% found this document useful (0 votes)
3 views6 pages

C^N Generics Examples

The document provides examples of various collection types in C#, including List, Dictionary, SortedSet, Queue, Stack, and HashSet. Each section demonstrates how to create and manipulate these collections, showcasing their unique functionalities. The code snippets illustrate adding, displaying, and removing elements from each collection type.

Uploaded by

yokaiyk1204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

C^N Generics Examples

The document provides examples of various collection types in C#, including List, Dictionary, SortedSet, Queue, Stack, and HashSet. Each section demonstrates how to create and manipulate these collections, showcasing their unique functionalities. The code snippets illustrate adding, displaying, and removing elements from each collection type.

Uploaded by

yokaiyk1204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

List
// Creating List using Constructors

using System;

using System.Collections.Generic;

class Geeks

public static void Main()

// default constructor creates an empty list

List<int> list = new List<int>();

list.Add(10);

list.Add(20);

Console.WriteLine("Default Constructor: ");

foreach (var item in list)

Console.WriteLine(item);

// Construnctors from IEnumerable

int[] num = { 10, 20 };

List<int> enumerableList = new List<int>(num);

Console.WriteLine("Constructor with IEnumerable: ");

foreach (var item in enumerableList)

Console.WriteLine(item);

// Constructor with Initial Capacity

List<int> Clist = new List<int>(2);

Clist.Add(10);
Clist.Add(20);

Console.WriteLine("Constructor with Initial Capacity: ");

foreach (var item in Clist)

Console.WriteLine(item);

2.Dictionary
// C# program to demonstrate how to

// create and display a dictionary

using System;

using System.Collections.Generic;

class Geeks

public static void Main()

// Creating a dictionary

Dictionary<int, string> sub = new Dictionary<int, string>();

// Adding elements

sub.Add(1, "C#");

sub.Add(2, "Javascript");

sub.Add(3, "Dart");

// Displaying dictionary

foreach (var ele in sub)

Console.WriteLine($"Key: {ele.Key}, Value: {ele.Value}");


}

3. SortedSet
// C# program to demonstrate the use of SortedSet

using System;

using System.Collections.Generic;

class Geeks {

public static void Main() {

// Creating a SortedSet

SortedSet<int> num =

new SortedSet<int> { 7, 1, 2, 8, 1, 4 };

// Adding elements

num.Add(6);

// Adding duplicate (will not be added)

num.Add(2);

// Displaying elements

Console.WriteLine("SortedSet elements:");

foreach (int ele in num)

Console.Write(ele + " ");

}
5.Queue
// C# program to demonstrates how to use Queue

using System;

using System.Collections.Generic;

class Geeks {

public static void Main(string[] args)

// Create a new queue

Queue<int> q = new Queue<int>();

// Enqueue elements into the queue

q.Enqueue(1);

q.Enqueue(2);

q.Enqueue(3);

q.Enqueue(4);

// Dequeue elements from the queue

while (q.Count > 0) {

Console.WriteLine(q.Dequeue());

6.Stack
// C# Program Implementing Stack Class

using System;

using System.Collections.Generic;

class Geeks
{

public static void Main(string[] args)

// Create a new stack

Stack<int> s = new Stack<int>();

// Push elements onto the stack

s.Push(1);

s.Push(2);

s.Push(3);

s.Push(4);

// Pop elements from the stack

while (s.Count > 0)

Console.WriteLine(s.Pop());

7.Hashset
// C# program to demonstrate HashSet functionality

using System;

using System.Collections.Generic;

class Geeks

public static void Main()

// Create a HashSet
HashSet<int> hs = new HashSet<int>();

// Add elements to the HashSet

hs.Add(10);

hs.Add(20);

hs.Add(30);

hs.Add(10);

// Display elements in the HashSet

Console.WriteLine("Elements in the HashSet: ");

foreach (int number in hs)

Console.WriteLine(number);

You might also like