Monday, August 22, 2022

C# Collections

Being in development for long period of time, I have  ended in repeating same code again and again. Unconsciously I started use list< c# collection> in most of my development. 

I used rarely other  collections. They were literally not exist to me. So I like to revisit the other  collections with you all :)


Dictionary

C# Dictionary is imported from the 'System.Collections.Generic' namespace and used to store generic collection of keys and values pair of data. Key must to unique, it will thrown ArgumentException when attempting to add a duplicate key.  And it will thrown KeyNotFoundException when attempting to retrieve value with key which is not present

To initializes a dictionary.

Dictionary<TKey,TValue> dict = new Dictionary<TKey,TValue> (); 

To add a element in dictionary

dict.Add(TKey, TValue)

To get an element in dictionary

dict.TryGetValue(TKey, out value)

To get number of items in dictionary

dict.Count

To Remove an item

dict.Remove(Tkey


More on this , Please refer msdn  https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=net-6.0


Queue 

A Queue in C# represents a first-in, first-out (FIFO) collection of objects. An example of a queue is a line of people waiting.

The Queue<T> class in the 'System.Collection.Generic namespace represents a queue in C#, where T specifies the type of elements in the queue..

 Objects stored in a Queue<T> are inserted at one end and removed from the other. Queues and stacks are useful when you need temporary storage for information; that is, when you might want to discard an element after retrieving its value. Use Queue<T> if you need to access the information in the same order that it is stored in the collection

To initializes a Queue

Queue<T> queue = newQueue<T>();

To get number of items in Queue

queue.Count()

To add a element in Queue

queue.Enqueue(T);

To retrieve and remove element in Queue

queue.Dequeue();

To retrieve without removing element in Queue

queue.Peek();


More on this ,  https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.queue-1?view=net-6.0


Stack

A Stack in C# represents a last-in, first-out (LIFO) collection of objects. Think of stack as a collection of items where anything you insert in a stack will be placed at the top and if you need to remove something, it will be removed from the top. A stack of plates or a book stack are two common examples of a stack

To initializes a Queue

Stack<T> stack= new Stack<T>();

To get number of items in stack
stack.Count

To add a element in stack
stack.Push(T);

To retrieve and remove element in stack
queue.Pop();

To retrieve without removing element in stack
stack.Peek();

More on this ,  https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.stack-1?view=net-6.0


Arrays  

You can store multiple variables of the same type in an array data structure. You declare an array by specifying the type of its elements. If you want the array to store elements of any type, you can specify object as its type. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object.

3 type of array as follow
  • Single dimensional array
  • Multi dimensional array
  • Jagged array

class TestArraysClass
{
    static void Main()
    {
        // Declare a single-dimensional array of 5 integers.
        int[] array1 = new int[5];

        // Declare and set array element values.
        int[] array2 = new int[] { 1, 3, 5, 7, 9 };

        // Alternative syntax.
        int[] array3 = { 1, 2, 3, 4, 5, 6 };

        // Declare a two dimensional array.
        int[,] multiDimensionalArray1 = new int[2, 3];

        // Declare and set array element values.
        int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };

        // Declare a jagged array.
        int[][] jaggedArray = new int[6][];

        // Set the values of the first array in the jagged array structure.
        jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
    }
}
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/

No comments:

Post a Comment

Duende IdentityServer ClientCredential flow

  Duende (Identity server)  is an OpenID Connect and OAuth 2.0 framework for ASP.NET Core. Duende Identity Server enables the following secu...