Found 2628 Articles for Csharp

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

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

4K+ 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

5K+ 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

54K+ 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

10K+ 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

5K+ 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

3K+ 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

How to convert an integer to string with padding zero in C#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 10:59:55

1K+ Views

There are several ways to convert an integer to a string in C#.PadLeft − Returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode characterToString − Returns a string that represents the current object.String Interpolation − The $ special character identifies a string literal as an interpolated string. This feature is available starting with C# 6.Example using string padding−Example Live Demousing System; namespace DemoApplication{    class Program{       public static void Main(){          int number = 5;         ... Read More

What is the difference between Last() and LastOrDefault() in Linq C#?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:37:18

630 Views

Both Last() and LastOrDefault() will fetch the last occurrence of a value. But the major difference between Last() and LastOrDefault() is that Last() will throw an exception if there is no result data for the supplied criteria whereas LastOrDefault() will return the default value (null) if there is no result data.Use Last() when we knew the sequence will have at least one element. Use LastOrDefault() when we are not sure about the data.Example Live Demousing System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace ConsoleApp {    public class Student {       public int Id { get; set; }   ... Read More

What is the use of yield return in C#?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:33:12

3K+ Views

Yield keyword helps to do custom stateful iteration over a collection. Meaning when we use yield keyword the control moves back and forth from the caller function to source and vice versa.Example Live Demousing System; using System.Collections.Generic; namespace DemoApplication {    class Program {       static List numbersList = new List {          1, 2, 3, 4, 5       };       public static void Main() {          foreach(int i in RunningTotal()) {             Console.WriteLine(i);          }          Console.ReadLine(); ... Read More

Advertisements