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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Single.CompareTo() Method in C# with Examples
The Single.CompareTo() method in C# is used to compare a float value with another float or object and returns an integer indicating the relative order. This method is essential for sorting operations and determining numerical relationships between single-precision floating-point numbers. Syntax The Single.CompareTo() method has two overloads − public int CompareTo(float value); public int CompareTo(object value); Parameters value − A float number or an object to compare with the current instance. Return Value The method returns an int value indicating the comparison result − ...
Read MoreConvert the value of the specified string to its equivalent Unicode character in C#
To convert the value of a specified string to its equivalent Unicode character in C#, you can use the Char.TryParse method. This method attempts to convert a string representation to a single Unicode character and returns a boolean indicating whether the conversion was successful. Syntax Following is the syntax for Char.TryParse method − public static bool TryParse(string s, out char result) Parameters s − The string to convert. Must be exactly one character long for successful conversion. result − When the method returns, contains the Unicode character equivalent if ...
Read MoreWhy cannot we specify access modifiers inside an interface in C#?
Interface members in C# traditionally cannot have access modifiers because interfaces define a contract that implementing classes must follow. The purpose of an interface is to specify what methods or properties a class must provide to the outside world, making all interface members implicitly public. Prior to C# 8.0, adding any access modifier to interface members would result in a compiler error. However, C# 8.0 introduced default interface methods, which allow access modifiers for specific scenarios involving method implementations within interfaces. Why Interface Members Are Public by Default Interfaces serve as contracts that define what functionality a ...
Read MoreWhat 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 More