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 112 of 2547
How to replace multiple spaces with a single space in C#?
There are several ways to replace multiple consecutive spaces with a single space in C#. This is a common task when cleaning up text data or normalizing whitespace in strings. The most effective approaches include using Regex.Replace() for pattern matching, string.Join() with Split() for splitting and rejoining, and string.Replace() for simple cases. Using Regex.Replace() The Regex.Replace() method uses regular expressions to find and replace patterns. The pattern \s+ matches one or more consecutive whitespace characters − using System; using System.Text.RegularExpressions; namespace DemoApplication { class Program { ...
Read MoreHow to implement Null object Pattern in C#?
The Null Object Pattern is a behavioral design pattern that helps eliminate null checks by providing a default object that implements the expected interface but performs no operations. Instead of returning null, you return a null object that behaves safely when methods are called on it. This pattern is particularly useful when you want to avoid NullReferenceException and make your code more readable by eliminating repetitive null checks. The null object provides a neutral behavior that represents "do nothing" or "no operation". Structure of Null Object Pattern Null Object Pattern Structure ...
Read MoreHow to add key/value pairs in SortedList in C#?
A SortedList in C# is a collection that stores key/value pairs sorted by keys. The Add() method is the primary way to insert key/value pairs into a SortedList, automatically maintaining sorted order based on the keys. Syntax Following is the syntax for adding key/value pairs to a SortedList − SortedList sortedList = new SortedList(); sortedList.Add(key, value); Parameters key − The key of the element to add. Cannot be null and must be unique. value − The value of the element to add. Can be null. Using ...
Read MoreConvert the value of the current DateTime object to a Windows file time in C#
The DateTime.ToFileTime() method in C# converts the value of the current DateTime object to a Windows file time. A Windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (UTC). This method is commonly used when working with file system operations, as Windows stores file timestamps in this format internally. Syntax Following is the syntax for the ToFileTime() method − public long ToFileTime() Return Value The method returns a long value representing the Windows file time equivalent of ...
Read MoreBitConverter.ToInt16() Method in C#
The BitConverter.ToInt16() method in C# is used to convert two consecutive bytes from a byte array into a 16-bit signed integer (short). This method reads bytes in little-endian format, where the first byte represents the lower-order bits and the second byte represents the higher-order bits. Syntax Following is the syntax for the BitConverter.ToInt16() method − public static short ToInt16(byte[] value, int startIndex); Parameters value − The byte array containing the bytes to convert. startIndex − The starting position within the byte array from which to begin reading two bytes. Return ...
Read MoreHow to validate an email address in C#?
Email validation is a crucial aspect of data validation in C# applications. There are several effective approaches to validate email addresses, each with its own advantages and use cases. Using MailAddress Class The MailAddress class from the System.Net.Mail namespace provides a simple way to validate email addresses by attempting to parse them. If the parsing succeeds, the email format is considered valid − Example using System; using System.Net.Mail; class EmailValidator { public static bool IsValidEmail(string email) { try { ...
Read MoreBitConverter.ToDouble() Method in C#
The BitConverter.ToDouble() method in C# is used to return a double-precision floating-point number converted from eight bytes at a specified position in a byte array. This method interprets 8 consecutive bytes as a 64-bit IEEE 754 floating-point number. Syntax The syntax is as follows − public static double ToDouble(byte[] value, int startIndex); Parameters value − An array of bytes containing the 8 bytes to convert. startIndex − The starting position within the byte array from which to begin reading the 8 bytes. Return Value Returns ...
Read MoreHow to create a SortedSet in C#?
A SortedSet in C# is a collection that maintains its elements in sorted order automatically. It belongs to the System.Collections.Generic namespace and ensures that duplicate elements are not allowed while keeping all elements sorted based on their natural ordering or a custom comparer. Syntax Following is the syntax for creating a SortedSet − SortedSet setName = new SortedSet(); You can also initialize with an existing collection − SortedSet setName = new SortedSet(existingCollection); To use a custom comparer − SortedSet setName = new SortedSet(comparer); Using SortedSet with ...
Read MoreConvert the specified double-precision floating point number to a 64-bit signed integer in C#
To convert a double-precision floating point number to its 64-bit signed integer representation, C# provides the BitConverter.DoubleToInt64Bits() method. This method converts the binary representation of a double value into a long integer without changing the underlying bit pattern. This is useful when you need to examine the internal binary structure of floating-point numbers or perform low-level operations on their bit representations. Syntax Following is the syntax for converting a double to its 64-bit signed integer representation − long result = BitConverter.DoubleToInt64Bits(doubleValue); Parameters The DoubleToInt64Bits() method takes one parameter − value: ...
Read MoreHow to get keys from a HashTable in C#?
The Hashtable is a non-generic collection in C# that stores key-value pairs, similar to the generic Dictionary collection. It is defined in the System.Collections namespace. A Hashtable consists of key/value pairs where each key is computed as a hash code and stored in different buckets internally. When accessing the Hashtable, this hash code is matched to locate the corresponding value, which optimizes lookup performance. Let's explore how to retrieve keys from a Hashtable in C#. Syntax Following is the syntax for accessing Hashtable keys using a foreach loop − foreach(DictionaryEntry entry in hashtable) { ...
Read More