Customers who sign-up prior to 30/06/2024 get unlimited access to free features, newer features (with some restrictions), but for free for at least 1 year.Sign up now! https://webveta.alightservices.com/
Categories
.Net C#

Some useful C# code snippets:

I have shown some code and discussed some general best practices earlier this morning in a live Youtube video – Some C# reusable code snippets (https://www.youtube.com/watch?v=9SyZjDukvhE) in ALight Technology And Service’s official Youtube channel (https://www.youtube.com/@alighttechnologyandservicesltd). As promised in the video, this blog post has the code snippets.

Some C# reusable code snippets

Concept – 1:

Centralizing config generation into a re-usable library, having a wrapper class around reading the config. Now the consuming classes do not need to know the details of where or how to get config values. For example, the config can be stored in plain-text / encrypted, can be stored in text files or something like AWS Secrets Manager.

public static void GenerateConfig()
{
    var retVal = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(<ConfigFile>, optional: false, reloadOnChange: true)
                .AddJsonFile(<ConfigFile2>, optional: false, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();

    ConfigHelper.Configuration = retVal;
}

Code for converting DateTime into Unix epoch and back to DateTime

private static DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0);

public static long GetUNIXDate(DateTime value)
{
    return (Int64)value.Subtract(epoch).TotalSeconds;
}

public static DateTime GetNormalDateFromUnix(long value)
{
    return epoch.AddSeconds(value);
}

Code for determining if a string consists entirely of ASCII text or not

public static bool IsASCII(this string value)
{
    return Encoding.UTF8.GetByteCount(value) == value.Length;
}

Code for removing non-ASCII characters and retrieving just the ASCII characters from a string

private static readonly Regex asciiRegex = new Regex(@"[^\u0000-\u007F]+", RegexOptions.Compiled);

public static string GetASCIIOnly(string value)
{
    if (value == null) return String.Empty;
    return asciiRegex.Replace(value, String.Empty);
}

Code for getting a smaller chunk of a long string and if necessary append … at the end – useful on web pages showing first few characters of a longer text.

public static string GetSnippet(string value, int length, bool appendDots)
{
    if (String.IsNullOrWhiteSpace(value)) return String.Empty;

    if (value.Length < length - 3) return value;

    if (appendDots)
    {
        return $"{value.Substring(0, length - 3)}...";
    }
    else
    {
        return value.Substring(0, length);
    }
}

I don’t have any fake aliases, nor any virtual aliases like some of the the psycho spy R&AW traitors of India. NOT associated – “ass”, eass, female “es”, “eka”, “ok”, “okay”, “is”, “erra”, yerra, karan, kamalakar, diwakar, kareem, karan, sowmya, zinnabathuni, bojja srinivas (was a friend and batchmate 1998 – 2002), mukesh golla (was a friend and classmate 1998 – 2002), thota veera, uttam’s, bandhavi’s, bhattaru’s, thota’s, bojja’s, bhattaru’s or Arumilli srinivas or Arumilli uttam (may be they are part of a different Arumilli family – not my family).

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

+44-33-3303-1284 (Preferred number if calling from U.K, No WhatsApp)

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.