The OrderedDictionary collection in C# maintains the insertion order of key-value pairs, combining features of both hashtables and arrays. To add key-value pairs, use the Add() method which accepts the key and value as parameters. OrderedDictionary is part of the System.Collections.Specialized namespace and allows access to elements both by key and by index, making it useful when you need to preserve insertion order. Syntax Following is the syntax for adding key-value pairs to an OrderedDictionary − OrderedDictionary dict = new OrderedDictionary(); dict.Add(key, value); Parameters key − The key of the element ... Read More
In C#, the Stack class is not thread-safe by default. To achieve synchronized access to a Stack in a multithreaded environment, you can use the SyncRoot property with the lock statement. This ensures that only one thread can access the Stack at a time, preventing data corruption and race conditions. Syntax Following is the syntax for synchronizing access to a Stack − lock(stack.SyncRoot) { // Thread-safe operations on stack foreach(Object item in stack) { // Process items safely } } ... Read More
Rotating a matrix by 90 degrees is a common operation in programming. When we need to rotate a matrix k times, we can optimize by noting that 4 rotations return the matrix to its original state. Therefore, we only need to perform k % 4 rotations. The algorithm works by processing concentric squares within the matrix. For an n×n matrix, there are n/2 concentric squares. In each square, elements move in a cycle of 4 positions during a 90-degree clockwise rotation. 90° Clockwise Rotation Pattern Original ... Read More
The Char.IsDigit() method in C# determines whether a specified Unicode character is categorized as a decimal digit. This method is useful for validating input, parsing strings, and filtering numeric characters from text. Syntax Following is the syntax for the Char.IsDigit() method − public static bool IsDigit(char ch); Parameters ch − The Unicode character to evaluate. Return Value Returns true if the character is a decimal digit; otherwise, false. Using Char.IsDigit() with Different Characters Example 1 - Testing Non-Digit Characters using System; public ... Read More
A HashSet in C# is a collection that stores unique elements without any specific order. To remove a specified element from a HashSet, you use the Remove() method, which returns true if the element was found and removed, or false if the element was not present. Syntax The Remove() method follows this syntax − public bool Remove(T item) Parameters item − The element to remove from the HashSet. Return Value The method returns true if the element was successfully found and removed; otherwise, false. ... Read More
Printing a matrix in spiral order means traversing the matrix elements layer by layer from the outermost to the innermost layer. We start from the top-left corner and move in a clockwise direction: right, down, left, and up, then repeat for the inner layers. Algorithm Steps Step 1 − Print all elements of the top row from left to right Step 2 − Print all elements of the rightmost column from top to bottom Step 3 − Print all elements of the bottom row from right to left Step 4 − Print all elements of the leftmost ... Read More
The Char.IsHighSurrogate(String, Int32) method in C# determines whether the character at the specified position in a string is a high surrogate. High surrogates are Unicode code units in the range U+D800 to U+DBFF, used as the first part of surrogate pairs to represent characters beyond the Basic Multilingual Plane. Syntax Following is the syntax − public static bool IsHighSurrogate(string str, int index); Parameters str − The string to evaluate. index − The position of the character to evaluate in the string. Return Value Returns true ... Read More
The RemoveRange() method in C# allows you to remove multiple elements from a List at once by specifying a starting index and the number of elements to remove. This is more efficient than removing elements one by one. Syntax Following is the syntax for the RemoveRange() method − public void RemoveRange(int index, int count) Parameters index − The zero-based starting index of the range of elements to remove. count − The number of elements to remove starting from the specified index. RemoveRange(1, 3) Example ... Read More
An island in a matrix is a group of connected cells containing '1' (representing land) that are surrounded by '0' (representing water). Two cells are considered connected if they are adjacent horizontally or vertically. This problem uses Depth-First Search (DFS) to traverse and mark all connected land cells as visited. The algorithm performs a linear scan of the 2D grid. When it finds a '1' that hasn't been visited, it triggers a DFS to explore the entire connected island and marks all cells in that island as visited. Each DFS call represents one complete island. Algorithm Steps ... Read More
The Char.IsLetter() method in C# is used to determine whether a specified Unicode character is categorized as a Unicode letter. This method returns true if the character is a letter (uppercase or lowercase), and false otherwise. This method is particularly useful for input validation, text processing, and character classification in applications where you need to distinguish letters from numbers, symbols, or other character types. Syntax Following is the syntax − public static bool IsLetter(char ch); Parameters ch − The Unicode character to evaluate. Return Value Returns ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance