Found 2587 Articles for Csharp

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

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:37:22

9K+ Views

A file can be downloaded from a URL using web client. It is available in 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 web client can be said as an application or web browser (like Google Chrome, Internet Explorer, Opera, Firefox, Safari) which is installed in a computer and used to interact with Web servers upon user’s request. It is basically a consumer application which collects processed data from servers.A Client and a Server are two parts of a connection, these are two ... Read More

How to populate XDocument from String in C#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:35:17

2K+ Views

XML is a self-describing language and it gives the data as well as the rules to identify what information it contains. Like HTML, XML is a subset of SGML - Standard Generalized Markup Language.The XDocument class contains the information necessary for a valid XML document. This includes an XML declaration, processing instructions, and comments.Note that we only have to create XDocument objects if we require the specific functionality provided by the XDocument class. In many circumstances, we can work directly with XElement. Working directly with XElement is a simpler programming model.XDocument derives from XContainer. Therefore, it can contain child nodes. ... Read More

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

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:31:26

5K+ Views

There are several ways to get the name of the current executable in C#.Using System.AppDomain −Application domain provides isolation between code running in different app domains. App Domain is a logical container for code and data just like process and has separate memory space and access to resources. App domain also serves as a boundary like process does to avoid any accidental or illegal attempts to access the data of an object in one running application from another.System.AppDomain class provides us the ways to deal with application domain. It provides methods to create new application domain, unload domain from memory ... Read More

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

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:27:58

5K+ Views

The desktop path of the current user can be fetched by using Environment.SpecialFolder. The Environment.SpecialFolder gets the path to the system special folder that is identified by the specified enumeration.string desktopPath =Environment.GetFolderPath(Environment.SpecialFolder.Desktop)The System.Environment Class provides information about the current environment and platform. The System.Environment Class uses to retrieve Environment variable settings, Version of the common language runtime, contents of the call stack etc. This class cannot be inherited.Environment class is static class which Provides the system configuration, Current program execution Environment as wel some properties for string manipulation such as news line, System Namespace represents the Environment Class.Environment class is ... Read More

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

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:24:37

7K+ Views

The best datatype to use for currency in C# is decimal. The decimal type is a 128-bit data type suitable for financial and monetary calculations. 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 b = 2.1m;The below example shows the min and max value of decimal.Example Live Demousing System; namespace DemoApplication{    public class Program{       public static void Main(){          Console.WriteLine($"Deciaml Min Value: {decimal.MinValue}");          Console.WriteLine($"Deciaml Max Value: {decimal.MaxValue}"); ... Read More

How to get only Date portion from DateTime object in C#?

Nizamuddin Siddiqui
Updated on 02-Sep-2023 13:12:30

65K+ Views

There are several ways to get only date portion from a DateTime object.ToShortDateString() − Converts the value of the current DateTime object to its equivalent short date string representation.Returns a string that contains the short date string representation of the current DateTime object.ToLongDateString() − Converts the value of the current DateTime object to its equivalent long date string representation.Returns a string that contains the long date string representation of the current DateTime object.ToString() − One more way to get the date from DateTime is using ToString() extension method.The advantage of using ToString() extension method is that we can specify the ... Read More

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

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:13:29

12K+ Views

Converting Integer to HexadecimalAn integer can be converted to a hexadecimal by using the string.ToString() extension method.Integer Value: 500 Hexadecimal Value: 1F4Converting Hexadecimal to Integer −A hexadecimal value can be converted to an integer using int.Parse or convert.ToInt32int.Parse − Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.Hexadecimal Value: 1F4 Integer Value: 500Convert.ToInt32 −Converts a specified value to a 32-bit signed integer.Hexadecimal Value: 1F4 Integer Value: 500Converting Integer to Hexadecimal −string hexValue = integerValue.ToString("X");Example Live Demousing System; namespace DemoApplication{    public class Program{       public static void ... Read More

How to validate an email address in C#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:08:58

6K+ Views

There are several ways to validate an email address in C#.System.Net.Mail −The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery.System.Text.RegularExpressions − Represents an immutable regular expression.Use the below expression@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1, 3}\.[0-9]{1, 3}\.[0-9]{1, 3}\.)|(([a-zA-Z0-9\-]+\.)+))([azA-Z]{2, 4}|[0-9]{1, 3})(\]?)$"We can use the MailAddress class of the System.Net.Mail namespace to validate an email addressExample Live Demousing System; using System.Net.Mail; namespace DemoApplication{    class Program{       public static void Main(){          try{             string email = "hello@xyzcom";             Console.WriteLine($"The email is {email}"); ... Read More

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

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:06:06

4K+ Views

There are several ways to replace multiple spaces with single space in C#.String.Replace − Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.Replace(String, String, Boolean, CultureInfo)String.Join Concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member.Regex.Replace −In a specified input string, replaces strings that match a regular expression pattern with a specified replacement string.Example using Regex −Example Live Demousing System; using System.Text.RegularExpressions; namespace DemoApplication{    class Program{       ... Read More

How to return multiple values to caller method in c#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:02:11

1K+ Views

A Tuple can be used to return multiple values from a method in C#. It allows us to store a data set which contains multiple values that may or may not be related to each other. A latest Tuple called ValueTuple is C# 7.0 (.NET Framework 4.7).ValueTuples are both performant and referenceable by names the programmer chooses. ValueTuple provides a lightweight mechanism for returning multiple values from the existing methods. ValueTuples will be available under System.ValueTuple NuGet package.public (int, string, string) GetPerson() { }Example 1using System; namespace DemoApplication{    class Program{       public static void Main(){     ... Read More

Advertisements