Csharp Articles

Page 59 of 196

How to replace multiple spaces with a single space in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 4K+ Views

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 More

How to validate an email address in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 6K+ Views

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 More

How to convert an integer to hexadecimal and vice versa in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 13K+ Views

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 More

What is the best data type to use for currency in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 7K+ Views

The best data type to use for currency in C# is decimal. The decimal type is a 128-bit data type specifically designed for financial and monetary calculations that require high precision and accuracy. The decimal type can represent values ranging from 1.0 × 10^-28 to approximately 7.9 × 10^28 with 28-29 significant digits. To initialize a decimal variable, use the suffix m or M − decimal currency = 2.1m; Why Decimal is Best for Currency Unlike float and double data types, decimal can represent fractional numbers like 0.1 exactly without rounding errors. Float and ...

Read More

How to get a path to the desktop for current user in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 6K+ Views

The desktop path of the current user can be fetched using Environment.GetFolderPath() with the Environment.SpecialFolder.Desktop enumeration. This method provides a reliable, cross-platform way to access special system folders. Syntax Following is the syntax for getting the desktop path − string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); The Environment.GetFolderPath() method takes an Environment.SpecialFolder enumeration value and returns the path to that special folder as a string. Parameters The method accepts the following parameter − folder − An Environment.SpecialFolder enumeration value that identifies the special folder whose path is to be retrieved. Return ...

Read More

How to get the name of the current executable in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 5K+ Views

There are several ways to get the name of the current executable in C#. Each method provides different levels of detail about the executable file, from just the name to the full path. Using System.AppDomain The AppDomain.CurrentDomain.FriendlyName property provides the name of the current application domain, which typically corresponds to the executable filename including its extension. Example using System; namespace DemoApplication { public class Program { public static void Main() { ...

Read More

How to populate XDocument from String in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

XML is a self-describing language that provides data along with rules to identify what information it contains. The XDocument class in C# contains all the information necessary for a valid XML document, including XML declarations, processing instructions, and comments. The XDocument class is available in the System.Xml.Linq namespace and derives from XContainer, allowing it to contain child nodes. However, XDocument objects can have only one child XElement node, reflecting the XML standard that permits only one root element per document. Syntax Following is the syntax to populate XDocument from a string using XDocument.Parse() − XDocument ...

Read More

How to download a file from a URL in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 10K+ Views

A file can be downloaded from a URL using WebClient, which is available in the System.Net namespace. The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI. The WebClient class uses the WebRequest class internally to provide access to web resources. It offers a simple, high-level interface for downloading files without dealing with complex HTTP protocols directly. Syntax Following is the syntax for downloading a file using WebClient.DownloadFile method − WebClient client = new WebClient(); client.DownloadFile("sourceUrl", "destinationPath"); Parameters ...

Read More

What is the difference between Task.WhenAll() and Task.WaitAll() in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 8K+ Views

The Task.WaitAll() and Task.WhenAll() methods in C# serve different purposes when working with multiple asynchronous tasks. Task.WaitAll() blocks the current thread until all tasks complete, while Task.WhenAll() returns a new task that represents the completion of all provided tasks without blocking the calling thread. Understanding the difference between these methods is crucial for building responsive applications, especially those with user interfaces where blocking the main thread can freeze the UI. Syntax Following is the syntax for Task.WaitAll() − Task.WaitAll(task1, task2, task3); // or with timeout Task.WaitAll(new Task[] { task1, task2 }, TimeSpan.FromSeconds(30)); Following ...

Read More

What is the difference between FromBody and FromUri attributes in C# ASP.NETnWebAPI?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 7K+ Views

In ASP.NET Web API, parameter binding determines how action method parameters receive values from HTTP requests. The [FromUri] and [FromBody] attributes control the source of parameter data, providing explicit control over this binding process. The FromUri attribute forces Web API to bind parameters from the URI query string, route data, or headers instead of the request body. The FromBody attribute instructs Web API to read parameter values from the HTTP request body using formatters like JSON or XML. Syntax Following is the syntax for using [FromUri] attribute − public IActionResult Method([FromUri] ModelClass model) { ...

Read More
Showing 581–590 of 1,951 articles
« Prev 1 57 58 59 60 61 196 Next »
Advertisements