In C#, you can check whether a thread is a background thread using the IsBackground property of the Thread class. Background threads are automatically terminated when all foreground threads complete, while foreground threads keep the application running until they finish execution. Syntax Following is the syntax to check if a thread is a background thread − bool isBackground = thread.IsBackground; To set a thread as a background thread − thread.IsBackground = true; Understanding Background vs Foreground Threads Background vs Foreground Threads ... Read More
A Hashtable collection in C# stores key-value pairs using a hash-based mechanism. Each key-value pair is organized based on the hash code of the key, which is calculated using a hash function. Unlike modern generic collections, hashtables can store objects of different data types but exhibit lower performance due to boxing and unboxing operations. In this article, we will explore how to add items to a hashtable collection using various methods available in the Hashtable class. Syntax The primary method for adding items to a hashtable is the Add() method − public virtual void Add(object ... Read More
The Decimal struct in C# represents a high-precision decimal floating-point number ideal for financial and monetary calculations. The Decimal value type represents decimal numbers ranging from positive 79, 228, 162, 514, 264, 337, 593, 543, 950, 335 to negative 79, 228, 162, 514, 264, 337, 593, 543, 950, 335. The default value of a Decimal is 0. The Decimal struct provides better precision than float or double for calculations where exact decimal representation is crucial, such as currency operations. Decimal vs Float Precision Comparison Decimal 0.1 + ... Read More
The SortedDictionary.ContainsValue() method in C# is used to determine whether the SortedDictionary contains an element with the specified value. This method performs a linear search through all values in the collection. Syntax Following is the syntax of the ContainsValue() method − public bool ContainsValue(TValue value) Parameters value − The value to be searched in the SortedDictionary. The value can be null for reference types. Return Value Returns true if the SortedDictionary contains an element with the specified value; otherwise, false. Using ContainsValue() to Check Existing Values The following example ... Read More
In C#, you can access the fifth element of a tuple using the Item5 property. This property is available for tuples that contain at least five elements. The Item5 property returns the value stored at the fifth position in the tuple. Syntax Following is the syntax to access the fifth element of a tuple − var fifthElement = tupleVariable.Item5; Where tupleVariable is a tuple with at least five elements, and Item5 retrieves the value at the fifth position. Using Item5 Property Example 1 The following example demonstrates how to access the ... Read More
The OrderedDictionary class in C# provides an Insert() method to add key-value pairs at a specific index position. Unlike regular dictionaries, OrderedDictionary maintains the insertion order of elements and allows indexed access. Syntax Following is the syntax for inserting into an OrderedDictionary at a specified index − orderedDict.Insert(index, key, value); Parameters index − The zero-based index at which to insert the key-value pair key − The key of the element to insert value − The value of the element to insert Using Insert() Method The following example demonstrates inserting ... Read More
IEnumerable is an interface that defines a single method GetEnumerator() and provides the foundation for iterating over collections. The List class is a concrete implementation that provides indexed access and manipulation methods for collections. Converting between these types is a common requirement in C# development. IEnumerable offers read-only iteration, while List provides full collection manipulation capabilities including adding, removing, and sorting elements. Converting IEnumerable to List The most efficient way to convert IEnumerable to List is using the ToList() extension method − Using ToList() Method using System; using System.Collections.Generic; using System.Linq; class Program ... Read More
The Console class in C# is part of the System namespace and provides methods and properties for interacting with the console window. It handles standard input, output, and error streams for console applications, allowing you to control cursor position, colors, buffer size, and other console characteristics. Syntax The Console class provides static properties and methods. Here are the basic syntax patterns − Console.PropertyName = value; // Setting properties var value = Console.PropertyName; // Getting properties Console.MethodName(parameters); // Calling methods ... Read More
The Queue.Enqueue() method in C# is used to add an object to the end of the Queue. This method follows the FIFO (First In, First Out) principle where elements are added at the rear and removed from the front. Syntax For generic Queue collections − public void Enqueue(T item); For non-generic Queue collections − public virtual void Enqueue(object obj); Parameters item/obj − The object to add to the end of the Queue. The value can be null for reference types. Queue.Enqueue() Operation ... Read More
The Hashtable class in C# provides the Remove() method to delete elements with a specified key. This method removes the key-value pair from the hashtable and reduces the count by one. Syntax Following is the syntax for the Remove() method − public virtual void Remove(object key) Parameters key − The key of the element to remove from the Hashtable. Using Remove() to Delete Single Element The following example demonstrates removing a single element from a Hashtable − using System; using System.Collections; public class Demo { ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance