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 by Nizamuddin Siddiqui
Page 12 of 196
How to convert an integer to hexadecimal and vice versa in C#?
Converting integers to hexadecimal and vice versa is a common requirement in C# programming. C# provides several built-in methods to perform these conversions efficiently using ToString() for integer-to-hex conversion and Convert.ToInt32() or int.Parse() for hex-to-integer conversion. Converting Integer to Hexadecimal An integer can be converted to hexadecimal using the ToString() method with format specifiers − Syntax string hexValue = integerValue.ToString("X"); // Uppercase string hexValue = integerValue.ToString("x"); // Lowercase string hexValue = integerValue.ToString("X8"); // 8-digit padding Example using System; public class Program { ...
Read MoreWhat are the benefits to marking a field as readonly in C#?
The readonly keyword in C# is used to declare a member variable as constant, but allows the value to be calculated at runtime. This differs from a constant declared with the const modifier, which must have its value set at compile time. Using readonly you can set the value of the field either in the declaration, or in the constructor of the object that the field is a member of. The readonly modifier provides several benefits including immutability after initialization, runtime value assignment, and thread safety for the field once set. Syntax Following is the syntax for ...
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 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 do you give a C# Auto-Property a default value?
In C#, auto-properties provide a shorthand way to declare properties without explicitly writing getter and setter methods. Setting default values for auto-properties can be done in two main ways: using constructor initialization (available in all C# versions) or using property initializers (introduced in C# 6.0). Syntax Constructor initialization syntax (C# 5.0 and earlier) − public class ClassName { public PropertyType PropertyName { get; set; } public ClassName() { PropertyName = defaultValue; ...
Read MoreHow to get all the files, sub files and their size inside a directory in C#?
To get all files and subdirectories within a directory in C#, the Directory.GetFiles method provides a comprehensive solution. This method returns the names of all files (including their full paths) that match a specified search pattern and can optionally search through subdirectories. The FileInfo class allows you to retrieve detailed information about each file, including its size, creation date, and other properties. Syntax Following is the syntax for using Directory.GetFiles − string[] files = Directory.GetFiles(path, searchPattern, searchOption); Parameters path − The directory path to search searchPattern − The search pattern (e.g., ...
Read More