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 705 of 2547
How to check if a number is a power of 2 in C#?
A power of 2 is a number of the form 2n where n is a non-negative integer. These numbers have a special property in their binary representation − they contain exactly one bit set to 1. For example, 8 = 23 has binary representation 1000, and 16 = 24 has binary representation 10000. n 2n Binary 0 1 0001 1 2 0010 2 4 0100 3 8 1000 4 16 10000 Bitwise Trick: n & ...
Read MoreC# Check if HybridDictionary is read only
The HybridDictionary class in C# provides a collection that uses a ListDictionary for small collections and switches to a Hashtable for larger collections. To check if a HybridDictionary is read-only, you can use the IsReadOnly property. Syntax Following is the syntax to check if a HybridDictionary is read-only − bool isReadOnly = hybridDictionary.IsReadOnly; Return Value The IsReadOnly property returns a bool value − true − if the HybridDictionary is read-only false − if the HybridDictionary allows modifications HybridDictionary Properties IsReadOnly ...
Read MoreHow to convert C# DateTime to "YYYYMMDDHHMMSS" format?
Converting a DateTime object to the "YYYYMMDDHHMMSS" format in C# is commonly needed for timestamps, file naming, and database operations. This format provides a compact, sortable representation of date and time without separators. The ToString() method with custom format strings allows you to convert DateTime objects to any desired string format using specific format specifiers. Syntax Following is the syntax for converting DateTime to "YYYYMMDDHHMMSS" format − DateTime dateTime = DateTime.Now; string formattedDate = dateTime.ToString("yyyyMMddHHmmss"); The format specifiers used are − yyyy − Four-digit year MM − Two-digit month (01-12) dd ...
Read MoreHow to get Second Element of the Tuple in C#?
In C#, tuples are data structures that hold multiple values of different types. To access the second element of a tuple, you use the Item2 property. This property is available for all tuples that have at least two elements. Syntax Following is the syntax for accessing the second element of a tuple − var secondElement = tuple.Item2; You can also use tuple deconstruction to extract specific elements − var (first, second, _) = tuple; // _ ignores third element Using Item2 Property The Item2 property provides direct access ...
Read MoreFirst occurrence in the List that matches the specified conditions in C#
To get the first occurrence in a list that matches the specified conditions in C#, you can use the Find() method. This method searches for an element that matches the conditions defined by the specified predicate and returns the first occurrence within the entire List. Syntax Following is the syntax for the Find() − public T Find(Predicate match) Parameters match − The Predicate delegate that defines the conditions to search for. Return Value Returns the first element that matches the conditions if found; otherwise, returns the default ...
Read MoreHow to create a folder if it does not exist in C#?
Creating directories programmatically is a common task in C# applications. The System.IO namespace provides the necessary classes and methods to check for directory existence and create new folders when needed. It is always recommended to check if a directory exists before performing any file operations in C#, as the compiler will throw an exception if you attempt to access a non-existent folder. Syntax Following is the syntax for checking if a directory exists − bool exists = Directory.Exists(path); Following is the syntax for creating a directory − Directory.CreateDirectory(path); Using ...
Read MoreHow to get the remaining elements of the Tuple in C#?
The Rest property in C# is used to get the remaining elements of a Tuple when it contains more than 7 elements. Since Tuple can hold a maximum of 8 elements, the 8th position (accessed via the Rest property) contains any additional elements as a nested Tuple. Syntax Following is the syntax for accessing the Rest property of a Tuple − var tuple = Tuple.Create(item1, item2, ..., item7, item8); var restElements = tuple.Rest; How It Works When a Tuple has exactly 8 elements, the Rest property returns the 8th element. When a Tuple ...
Read MoreRemove element at specified index of Collection in C#
To remove an element at a specified index from a Collection in C#, you use the RemoveAt() method. This method removes the element at the specified zero-based index and shifts all subsequent elements down by one position. Syntax Following is the syntax for the RemoveAt() method − collection.RemoveAt(index); Parameters index − The zero-based index of the element to remove. Must be a valid index within the collection bounds. Using RemoveAt() for Single Element Removal The following example demonstrates removing a single element at index 3 from a Collection − ...
Read MoreHow to get the Standard Input and Output Stream through Console in C#?
The Console class in C# provides three standard streams for input and output operations: Console.In for standard input, Console.Out for standard output, and Console.Error for standard error. These streams allow you to access the underlying TextReader and TextWriter objects used by the console. Standard Streams Overview The console streams are represented as properties that return the following types − Console.In − Returns a TextReader object for reading standard input Console.Out − Returns a TextWriter object for writing to standard output Console.Error − Returns a TextWriter object for writing to standard error ...
Read MoreWhat is the difference between Func delegate and Action delegate in C#?
A delegate is a type that represents references to methods with a particular parameter list and return type. When we instantiate a delegate, we can associate its instance with any method with a compatible signature and return type. We can invoke (or call) the method through the delegate instance. The .NET Framework provides two built-in generic delegate types: Func and Action. Both serve different purposes based on whether the method returns a value or not. Func Delegate The Func delegate is a generic delegate included in the System namespace. It has zero or more input parameters and ...
Read More