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 9 of 196
How do I get a human-readable file size in bytes abbreviation using C#?
To get a human-readable file size in bytes with proper abbreviations, C# requires converting raw byte values into appropriate units like KB, MB, GB, or TB. This involves dividing by 1024 (or 1000 depending on your preference) and selecting the most appropriate unit to display. The key is to create a method that automatically determines the best unit and formats the size accordingly, rather than showing all units simultaneously. Syntax To get file size in bytes − long sizeInBytes = new FileInfo(filePath).Length; To format the size into human-readable format − string ...
Read MoreWhat does the two question marks together (??) mean in C#?
The null-coalescing operator (??) in C# returns the value of its left-hand operand if it isn't null; otherwise, it evaluates and returns the right-hand operand. This operator provides a concise way to handle null values and assign default values. The ?? operator is particularly useful with nullable types, which can represent either a value from the type's domain or be undefined (null). It helps prevent InvalidOperationException exceptions and reduces the need for verbose null-checking code. Syntax Following is the syntax for the null-coalescing operator − result = leftOperand ?? rightOperand; If leftOperand is ...
Read MoreWhat is connection pooling in C# and how to achieve it?
Connection pooling in C# is a technique that improves database application performance by reusing database connections rather than creating new ones for each request. When a connection is closed, it's returned to a pool for future use instead of being destroyed. The .NET Framework automatically manages connection pooling for ADO.NET connections. When you use the using statement with database connections, it ensures proper disposal and automatic participation in connection pooling. How Connection Pooling Works Connection Pool Lifecycle Application Connection Pool ...
Read MoreWhat is @ in front of a string in C#?
The @ symbol in front of a string in C# creates a verbatim string literal. This special prefix tells the compiler to treat the string exactly as written, ignoring escape sequences and preserving formatting including line breaks. In C#, a verbatim string is created using the @ symbol as a prefix before the opening quote. The compiler identifies this as a verbatim string and processes it literally. The main advantage of the @ symbol is to tell the string constructor to ignore escape characters and preserve line breaks exactly as they appear in the source code. Syntax ...
Read MoreHow 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 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 MoreWhich is better System.String or System.Text.StringBuilder classes in C#?
The main difference between System.String and System.Text.StringBuilder is that StringBuilder is mutable whereas String is immutable. String is immutable, meaning once you create a string object, you cannot modify it. Any operation that appears to change a string actually creates a new string object in memory. On the other hand, StringBuilder is mutable. When you create a StringBuilder object, you can perform operations like insert, replace, or append without creating a new instance each time. It updates the string content in the same memory location. Memory Allocation Comparison String vs StringBuilder Memory ...
Read MoreHow 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 More