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
Csharp Articles
Page 59 of 196
How 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 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 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 is the best data type to use for currency in C#?
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 MoreHow to get a path to the desktop for current user in C#?
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 MoreHow to get the name of the current executable in C#?
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 MoreHow to populate XDocument from String in C#?
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 MoreHow to download a file from a URL in C#?
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 MoreWhat is the difference between Task.WhenAll() and Task.WaitAll() in C#?
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 MoreWhat is the difference between FromBody and FromUri attributes in C# ASP.NETnWebAPI?
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