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 11 of 196
What is the difference between int and Int32 in C#?
Int32 is a type provided by .NET framework whereas int is an alias for Int32 in C# language. Both represent 32-bit signed integers and compile to identical code, making them functionally equivalent at runtime. Syntax Following is the syntax for declaring integers using both approaches − Int32 variable1 = value; int variable2 = value; Key Differences Aspect int Int32 Type C# language keyword (alias) .NET Framework type Namespace dependency No namespace required Requires System namespace Compilation Compiles to System.Int32 Direct .NET type ...
Read MoreHow to return multiple values to caller method in c#?
Returning multiple values from a method in C# can be achieved through several approaches. The most modern and efficient approach is using ValueTuple, introduced in C# 7.0, which provides a lightweight mechanism for returning multiple values with optional named elements. ValueTuples are both performant and allow referencing by names the programmer chooses. They are available under the System.ValueTuple NuGet package for older framework versions. Syntax Following is the syntax for declaring a method that returns multiple values using ValueTuple − public (int, string, string) MethodName() { return (value1, value2, value3); } ...
Read MoreHow 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 More