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 44 of 2547
What is an alternative to string.Replace that is case-insensitive in C#?
The standard string.Replace() method in C# is case-sensitive by default. When you need case-insensitive string replacement, there are several alternative approaches you can use. The most common methods include using regular expressions, the StringComparison parameter (available in .NET 5+), or creating custom replacement methods. Using Regular Expressions for Case-Insensitive Replace Regular expressions provide the most flexible approach for case-insensitive string replacement using RegexOptions.IgnoreCase − using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { string str = "Cricket Team"; ...
Read MoreHow to implement IDisposable Design Pattern in C#?
The IDisposable design pattern (also called the Dispose Pattern) in C# is used to properly clean up unmanaged resources like file handles, database connections, and network streams. This pattern ensures that resources are released deterministically, rather than waiting for the garbage collector. Classes that directly or indirectly use unmanaged resources should implement the IDisposable interface. This includes classes that use FileStream, HttpClient, database connections, or any other objects that hold system resources. Syntax Following is the basic syntax for implementing IDisposable − public class ClassName : IDisposable { private bool disposed = ...
Read MoreBitConverter.ToInt32() Method in C#
The BitConverter.ToInt32() method in C# is used to convert four bytes from a byte array at a specified position into a 32-bit signed integer. This method reads four consecutive bytes and interprets them as an integer value according to the system's endianness (little-endian on most systems). Syntax Following is the syntax for the BitConverter.ToInt32() method − public static int ToInt32(byte[] value, int startIndex); Parameters value − A byte array containing the bytes to convert. startIndex − The starting position within the value array (zero-based index). Return Value Returns a ...
Read MoreReinterpret 64-bit signed integer to a double-precision floating point number in C#
The BitConverter.Int64BitsToDouble() method in C# reinterprets the bit pattern of a 64-bit signed integer as a double-precision floating point number. This is not a numeric conversion − it treats the integer's binary representation as if it were the IEEE 754 double format. Syntax Following is the syntax for the Int64BitsToDouble method − public static double Int64BitsToDouble(long value) Parameters value − A 64-bit signed integer whose bit pattern will be reinterpreted as a double. Return Value Returns a double-precision floating point number whose bit representation is equivalent to the input ...
Read MoreHow to Get Value from HashTable Collection in C# using Specified Key
A Hashtable is a collection of key-value pairs that provides fast lookup by key. In C#, you can retrieve values from a hashtable using the indexer syntax hashtable[key] or by checking if a key exists using the Contains() method. This is essential for accessing specific data when you know the corresponding key. Syntax Following is the syntax for getting a value from a hashtable using a specified key − // Check if key exists if (hashtable.Contains(key)) { object value = hashtable[key]; } // Direct access (may return null if key doesn't exist) ...
Read MoreHow can we return multiple values from a function in C#?
In C#, there are several approaches to return multiple values from a function. This is useful when you need to return more than one result from a single method call. The main approaches are − Reference parameters using ref keyword Output parameters using out keyword Returning an Array of values Returning a Tuple object Using Reference Parameters The ref keyword passes arguments by reference, allowing the method to modify the original variable. The variable must be initialized before passing it to the method. Syntax ...
Read MoreHow to get only Date portion from DateTime object in C#?
There are several ways to extract only the date portion from a DateTime object in C#. Each method serves different purposes depending on whether you need a string representation or want to preserve the DateTime type. Methods Overview Method Return Type Description ToShortDateString() String Returns culture-specific short date format ToLongDateString() String Returns culture-specific long date format ToString(format) String Returns custom formatted date string DateTime.Date DateTime Returns DateTime with time set to 00:00:00 Using ToShortDateString() and ToLongDateString() These methods provide culture-specific ...
Read MoreHow to parse a string into a nullable int in C#?
Parsing a string into a nullable int in C# allows you to handle cases where the string might not represent a valid integer. A nullable int (int?) can store either an integer value or null, making it ideal for scenarios where conversion might fail. C# provides several approaches to parse strings into nullable integers, from extension methods to built-in parsing techniques that handle invalid input gracefully. Syntax Following is the syntax for declaring a nullable int − int? nullableInt = null; int? nullableInt = 42; Following is the syntax for parsing using int.TryParse() ...
Read MoreHow to change the CursorTop of the Console in C#?
The Console.CursorTop property in C# gets or sets the row position of the cursor within the console window. This property allows you to control the vertical position where text will be displayed, enabling you to create formatted console output, menus, or position text at specific locations. Syntax Following is the syntax for using Console.CursorTop − // Get current cursor top position int currentTop = Console.CursorTop; // Set cursor top position Console.CursorTop = value; Parameters The Console.CursorTop property accepts an integer value representing the row position (0-based) where the cursor should be positioned. ...
Read MoreSByte.ToString() Method in C# with Examples
The SByte.ToString() method in C# is used to convert the numeric value of a signed byte to its equivalent string representation. The sbyte data type represents 8-bit signed integers with values ranging from -128 to 127. Syntax Following is the syntax for the basic ToString() method − public override string ToString(); Following is the syntax for ToString() with format provider − public string ToString(IFormatProvider provider); public string ToString(string format); public string ToString(string format, IFormatProvider provider); Return Value The method returns a string that represents the value of the current ...
Read More