Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 9 of 2547
Create a Stack from a collection in C#
To create a Stack from a collection in C#, you can use the Stack constructor that accepts an IEnumerable parameter. This allows you to initialize a stack with elements from arrays, lists, or other collections. Syntax Following is the syntax for creating a Stack from a collection − Stack stack = new Stack(IEnumerable collection); The elements are pushed in the reverse order of the collection enumeration. Creating Stack from Array The following example shows how to create a Stack from an existing array − using System; using System.Collections.Generic; public ...
Read MoreCheck if OrderedDictionary collection is read-only in C#
The OrderedDictionary class in C# provides a collection that maintains the order of key-value pairs while allowing access by both key and index. To check if an OrderedDictionary is read-only, you use the IsReadOnly property. An OrderedDictionary is typically not read-only by default, meaning you can add, modify, and remove elements after creation. However, you can create a read-only wrapper using specific methods. Syntax Following is the syntax to check if an OrderedDictionary is read-only − bool isReadOnly = orderedDictionary.IsReadOnly; Following is the syntax to create a read-only wrapper − OrderedDictionary ...
Read MoreHow to implement Fibonacci using topDown approach using C#?
The Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the preceding two numbers. The top-down approach focuses on breaking down a big problem into smaller and understandable chunks using memoization to store previously computed values. This approach uses recursion with an additional array to cache results, avoiding redundant calculations and significantly improving performance over naive recursion. Complexity Analysis Time complexity − O(N) because each Fibonacci number is ...
Read MoreAdding new node or value at the start of LinkedList in C#
To add a new node or value at the start of a LinkedList in C#, you can use the AddFirst() method. This method adds the specified element at the beginning of the LinkedList, making it the first node. Syntax Following is the syntax for adding elements to the beginning of a LinkedList − linkedList.AddFirst(value); You can also add a LinkedListNode object − linkedList.AddFirst(new LinkedListNode(value)); Parameters The AddFirst() method accepts the following parameter − value − The value to add at the beginning of the LinkedList ...
Read MoreHow to use "not in" query with C# LINQ?
In C# LINQ, there are several ways to perform "not in" queries to exclude items from one collection that exist in another. The most common approaches use the Except operator, the Where clause with Any, or the Contains method. These operators work with any data source that implements IEnumerable, making them versatile for querying collections, arrays, and LINQ query results. Syntax Using the Except operator − var result = collection1.Except(collection2); Using Where with Any − var result = from item in collection1 ...
Read MoreCreating a Case-Sensitive HybridDictionary with specified initial size in C#
The HybridDictionary class in C# is a collection that combines the benefits of both ListDictionary and Hashtable. It automatically switches from a ListDictionary (for small collections) to a Hashtable (for larger collections) based on the number of elements. You can create a case-sensitive HybridDictionary with a specified initial size using its constructor. Syntax Following is the syntax for creating a HybridDictionary with specified initial size and case sensitivity − HybridDictionary myDict = new HybridDictionary(initialSize, caseInsensitive); Parameters initialSize − The approximate number of entries that the HybridDictionary can initially contain. ...
Read MoreAdding the elements of the specified collection to the end of the List in C#
The AddRange() method in C# allows you to add all elements from a specified collection to the end of a List. This method is more efficient than adding elements one by one using Add() when you need to append multiple items from an existing collection. Syntax Following is the syntax for the AddRange() method − public void AddRange(IEnumerable collection) Parameters collection − The collection whose elements should be added to the end of the List. The collection itself cannot be null, but it can contain elements that are null. Using ...
Read MoreHow to implement minimum step to one using topDown approach using C#?
The minimum steps to one problem asks: given a positive integer n, find the minimum number of steps to reduce it to 1 using these operations − If n is divisible by 3, divide it by 3 If n is divisible by 2, divide it by 2 Subtract 1 from n The top-down approach uses recursion with memoization to solve this problem efficiently by storing computed results in a DP array. How It Works The algorithm recursively explores all possible operations at each step and chooses the one that gives the minimum result. Memoization ...
Read MoreCreating a HybridDictionary with specified initial size in C#
The HybridDictionary class in C# is a special collection that automatically switches between a ListDictionary for small collections and a Hashtable for larger ones. When creating a HybridDictionary with a specified initial size, you can optimize performance by indicating the expected number of elements. The HybridDictionary uses a ListDictionary when the collection is small (typically less than 10 items) and switches to a Hashtable when it grows larger, providing the best performance characteristics for both scenarios. Syntax Following is the syntax for creating a HybridDictionary with specified initial size − HybridDictionary dict = new HybridDictionary(initialSize); ...
Read MoreAdding the specified key and value into HybridDictionary in C#
The HybridDictionary class in C# provides a collection that combines the functionality of a ListDictionary for small collections and a Hashtable for larger collections. It automatically switches between these internal implementations based on the number of elements to optimize performance. HybridDictionary is part of the System.Collections.Specialized namespace and uses a ListDictionary when the collection is small (typically under 10 items) and switches to a Hashtable when it grows larger. Syntax Following is the syntax for adding key-value pairs to a HybridDictionary − HybridDictionary dictionary = new HybridDictionary(); dictionary.Add(key, value); Parameters key ...
Read More