devxlogo

C#

How Reverse a String in C#

See how to reverse a string. public static string ReverseString( string input) { char[] array = input.ToCharArray(); Array.Reverse(array); return new string(array); }Console.WriteLine(ReverseString(“devx”));

Format the Currency for a Culture in C#

Use the CultureInfo class to format values based on a culture. See below for an example: public static void Main() { decimal inputValue = 233; CultureInfo currentCulture = new CultureInfo(“en-US”);

Countdown Timer Function in C#

See how to use this function in C# to discover how many more days are left in a given countdown. DateTime startDate = DateTime.Now;DateTime endDate = DateTime.Now.AddDays(-1);TimeSpan t = startDate

Convert a Date to ISO 8601 Format in C#

Please see below for an example of how to convert a date time to ISO 8601 format. string isoFormatDateString = dateTimeObject.ToUniversalTime().ToString(“s”) + “Z”;

Check Whether or Not an IP Is Valid in C#

See below for a short function that validates a given IP address: public static Boolean ValidateIP(string inputIP){var ipParts = inputIP.Split(‘.’);return Int32.Parse(ipParts [0]) < 256 &&Int32.Parse(ipParts [1]) < 256&Int32.Parse(ipParts [2]) <

Convert Hex to RGB in C#

The .NET System.Globalizaton’s NumberStyles has a parameter called AllowHexSpecifier that helps us to replace a hexString with RGB color. public static Color HexToColor(string hexString){ //replace # occurences if (hexString.IndexOf(‘#’) !=

Change Date to ISO8601 Date Format in C#

To change the date format in C#, just use the ToUniverstalTime and the overload formatting as below: String isoFormat = inputDateTime.ToUniversalTime().ToString(“s”) + “Z”;

Calculate the Distance Between Two Coordinates in C#

We can use the GetDistinaceTo?method of the GetCoordinates?class to determine the distance between two coordinates in C#. See below for an example: Using System.Device.Location;//double distance = 0.0;var aCoordinate = new

Get a Special Folder Path in C#

Use the System.Environment namespace’s GetFolderPath?method to retrieve the path of a special folder. It can also create the folder if it does not exist. System.Environment.GetFolderPath(Environment.SpecialFolder.Recent, Environment.SpecialFolderOption.None))