Skip to content

Extensions.dll contains extension methods that enhance existing C# classes thus making life easier for developers.

License

Notifications You must be signed in to change notification settings

cjvandyk/Extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Extensions.cs contains extension methods that enhance existing C# classes thus making life easier for developers.

icon GIF GIF License Maintained GitHub Release NuGet Badge Repo Size Closed Issues Open Issues Contributors Languages Gitter Discord Twitter

The following classes have been extended:

- System.Array
- System.Collections.Generic.Dictionary
- System.Collections.Generic.List
- System.DateTime
- System.Diagnostics.Process
- System.Double
- System.Int16
- System.Int32
- System.Int64
- System.Logging (added)
- System.Long
- System.Net.WebException
- System.Object
- System.String
- System.Text.StringBuilder
- System.Timer
- System.TimeZoneInfo
- System.UInt16
- System.UInt32
- System.UInt64
- System.ULong

with these methods:

  • BeginsWith()

    Reduces checking if a given string starts with another given string
    from this:
    (str.ToLower().Substring(0, target.Length) == target.ToLower())
    to this:
    str.BeginsWith(target, true)

    GIF

  • Bigest()

    Return the bigest of two given values.
    For example:
    Bigest(23, 31)
    will return
    31

  • CompoundInterest()

    _Calculate compounded interest end value given an amount, percent
    interest per year and number of years.
    For example:
    double val = 100.00;
    val.CompoundInterest(5,
    10,
    Constants.CompoundFrequency.Yearly);
    will return 162.889462677744 _

  • ContainsAny()

    Checks if the given string contains any of a list of characters or
    strings provided.
    For example:
    "abcdef1234567890".ContainsAny(Constants.HexChars)
    will return True.

    GIF

  • ContainsOnly()

    Checks if the given string contains only characters or strings
    in the list provided.
    For example:
    "abcdef1234567890".ContainsOnly(Constants.HexChars)
    will return True while
    "abcdefg1234567890".ContainsOnly(Constants.HexChars)
    will return False because of the "g".

  • CopyTo()

    Copies a given length of bytes from a byte[] starting at a definable
    offset.
    For example:
    byte[] b1 = System.Text.Encoding.UTF8.GetBytes("blog.cjvandyk.com rocks!");
    byte[] b2 = b1.CopyTo(10);
    byte[] b3 = b1.CopyTo(10, 5);
    will result in the following arrays:
    98 108 111 103 46 99 106 118 97 110 100 121 107 46 99 111 109 32 114 111 99 107 115 33
    98 108 111 103 46 99 106 118 97 110
    99 106 118 97 110 100 121 107 46 99

  • DoubleQuote()

    Return the given string encased in double quotes.
    For example:
    printf("https://blog.cjvandyk.com/sites/Rocks");
    printf("https://blog.cjvandyk.com/sites/Rocks".DoubleQuote());
    will return
    https://blog.cjvandyk.com/sites/Rocks
    "https://blog.cjvandyk.com/sites/Rocks"

  • Elevate()

    Restarts the current process with elevated permissions.
    For example:
    System.Diagnostics.Process.GetCurrentProcess().Elevate(args)
    will restart the current console app in admin mode.

  • Err()

    Write an Error message to active channels (console, event log, file)
    using the System.Logging class.

  • ExceedsLength()

    Checks if a referenced offset exceeds the length of the string.
    Optionally increments the offset as well.
    For example:
    "https://blog.cjvandyk.com Rocks!".ExceedsLength(30)
    will return False while
    "https://blog.cjvandyk.com Rocks!".ExceedsLength(31, false)
    will also return False and
    "https://blog.cjvandyk.com Rocks!".ExceedsLength(31)
    will return True.

  • Get()

    Language extension for properties. Use to set the value of the
    extension property in question.
    For example:
    Microsoft.SharePoint.Client client = new Microsoft.SharePoint.Client("https://blog.cjvandyk.com");
    client.ExecutingWebRequest += ClientContext_ExecutingWebRequest;
    client.Set("HeaderDecoration", "NONISV|Crayveon|MyApp/1.0");
    This allows the creation of the extension property "HeaderDecoration"
    which can be changed as needed. Later in the delegate method we
    refer back to the extension property value thus:
    private void ClientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
    {
    e.WebRequestExecutor.WebRequest.UserAgent = (string)e.Get("HeaderDecoration");
    }
    NOTE: We did not have to access the ClientContext class in order to
    retrieve the "HeaderDecoration" value since the extension was
    done against the System.Object class. As such, any object can
    be used to retrieve the extension property value, as long as
    you know the key value under which the property was stored and
    you know the type to which the returned value needs to be cast.
    A derived override method for Get() and Set() can be defined
    using specific class objects if finer controls is needed.

  • GetExecutingAssembly()

    Gets the current Entry or Executing assembly through reflection.

  • GetExecutingAssemblyName()

    Gets the name of the current assembly, optionally escaped.

  • GetExecutingAssemblyFolder()

    Gets the folder location of the current assembly, optionally escaped.

  • GetExecutingAssemblyFullPath()

    Gets the full path and file name of the current assembly, optionally
    escaped.

  • GetFQDN()

    Get the current computer Fully Qualified Domain Name.

  • GetNthPrime()

    Get the Nth prime number. It will serialize the list of discovered
    prime numbers to file in order to eliminate duplicate calculation
    of prime numbers. Use Universal.PrimeStatePath to override the
    path where the discovered list of prime numbers is saved.
    For example:
    Extensions.Universal.GetNthPrime(1000)
    will return the 1000th prime number - 7919.

  • GetNthPrimeAsync()

    Get the Nth prime number using multi threading and asynchronous
    processing. It will serialize the list of discovered
    prime numbers to file in order to eliminate duplicate calculation
    of prime numbers. Use Universal.PrimeStatePath to override the
    path where the discovered list of prime numbers is saved.
    For example:
    Extensions.Universal.GetNthPrime(1000)
    will return the 1000th prime number - 7919.

  • GetSiteUrl()

    Given a full SharePoint Online object URL, this method will return
    the site collection part of the URL.
    For example:
    "https://crayveon.sharepoint.com/sites/TheSite/lists/TheList".GetTenantUrl()
    would return https://crayveon.sharepoint.com/sites/TheSite

  • GetTenantUrl()

    Given a full SharePoint Online object URL, this method will return
    only the tenant part of the URL.
    For example:
    "https://crayveon.sharepoint.com/sites/TheSite/lists/TheList".GetTenantUrl()
    would return https://crayveon.sharepoint.com

  • GetTimeZoneString()

    Get the registry ID string that can be used with
    TimeZoneInfo.FindSystemTimeZoneById() for time zone convertions.
    For example:
    System.TimeZoneInfo.FindSystemTimeZoneById(
    Extensions.TimeZoneInfo.GetTimeZoneString(
    Constants.TimeZone myZone))
    will return the proper string to use in the call.

  • GetUrlRoot()

    Get the URL root for the given string object containing a URL.
    For example:
    "https://blog.cjvandyk.com".GetUrlRoot()
    will return "https://blog.cjvandyk.com" whereas
    "https://blog.cjvandyk.com/sites/Approval".GetUrlRoot()
    will also return "https://blog.cjvandyk.com".

  • HasLower()

    Validates that the given string object contains a lower case character.
    For example:
    "abc".HasLower()
    will return True whereas
    "ABC".HasLower()
    will return False and
    "AbC".HasLower()
    will return True.

  • HasNumeric()

    Validates that the given string object contains a number character.
    For example:
    "abc".HasNumeric()
    will return False whereas
    "ABC123".HasNumeric()
    will return True and
    "A2C".HasNumeric()
    will return True.

  • HasSymbol()

    Validates that the given string object contains a symbol or special
    character.
    For example:
    "abc".HasSymbol()
    will return False whereas
    "ABC$".HasSymbol()
    will return True and
    "A@C".HasSymbol()
    will return True.

  • HasUpper()

    Validates that the given string object contains a lower case character.
    For example:
    "abc".HasUpper()
    will return False whereas
    "ABC".HasUpper()
    will return True and
    "AbC".HasUpper()
    will return True.

  • HtmlDecode()

    Decode the HTML escaped components in a given string returning the
    given source string without HTML escaped components.
    For example:
    "https://blog.cjvandyk.com/sites/Rocks <&> Rolls!".HtmlDecode()
    will return
    https://blog.cjvandyk.com/sites/Rocks <&> Rolls!

  • HtmlEncode()

    Encode the given string to be HTML safe.
    For example:
    "https://blog.cjvandyk.com/sites/Rocks <&> Rolls!".HtmlEncode()
    will return
    https://blog.cjvandyk.com/sites/Rocks &lt;&amp;&gt; Rolls!

  • Inf()

    Write an Information message to active channels (console, event log, file)
    using the System.Logging class.

  • IsAlpha()

    Validates that the given string object contains all alphabetic
    characters (a-z and A-Z) returning True if it does and False if
    it doesn't.
    For example:
    "abcXYZ".IsAlphabetic()
    will return True whereas
    "abc123".IsAlphabetic()
    will return False.

  • IsAlphaNumeric()

    Validates that the given string object contains all alphabetic
    and/or numeric characters (a-z and A-Z and 0-9) returning True if it
    does and False if it doesn't.
    For example:
    "abc123".IsAlphaNumeric()
    will return True whereas
    "abcxyz".IsAlphaNumeric()
    will also return True and
    "123456".IsAlphaNumeric()
    will also return True but
    "abc!@#".IsAlphaNumeric()
    will return False.

  • IsChar()

    This method takes a char[] as one of its arguments against which the
    given string object is validated. If the given string object contains
    only characters found in the char[] it will return True, otherwise it
    will return False.
    For example:
    "aacc".IsChar(new char[] {'a', 'c'})
    will return True whereas
    "abc123".IsChar(new char[] {'a', 'c'})
    will return False.

  • IsDateTime()

    Validates that the given string is a valid date/time given the format.
    It provides an easy way to validate date string input.
    For example these:
    "20170704033333".IsDateTime("yyyMMddHHmmss")
    "07/27/2017 03:33:33".IsDateTime("MM/dd/yyyy HH:mm:ss")
    "27/07/2017 03:33:33".IsDateTime("dd/MM/yyyy HH:mm:ss")
    "27/07/2017".IsDateTime("dd/MM/yyyy")
    will all return True whereas these:
    "2017070403333".IsDateTime("yyyMMddHHmmss")
    2017/07/04 03:33:3 is clearly missing another digit.
    "07/27/2017".IsDateTime("dd/MM/yyyy HH:mm:ss")
    07/27 fails because there isn't 27 months.
    "27/07/2017".IsDateTime("MM/dd/yyyy")
    27/07 fails for the same reason.
    will return False.

  • IsEmail()

    Validates that the given string object contains a valid email address.
    For example:
    "[email protected]".IsEmail()
    will return True whereas
    "noreplay-at-crayveon.com".IsEmail()
    will return False.

  • IsEven()

    Checks if the given number is even.
    For example:
    234.IsEven()
    will return True whereas
    339.IsEven()
    will return False.

  • IsHex()

    Checks if the given string represents hex based numbers.
    For example:
    "9723FDC".IsHex()
    will return True whereas
    "9723FDT.IsHex()
    will return False because T is not a hex character.

  • IsLower()

    Validates that the given string object contains only lower case letters.
    For example:
    "IsLower test".IsLower()
    will return False while
    "islower test".IsLower()
    will return True and
    "islower test".IsLower(false)
    will return False.

  • IsNumeric()

    Validates that the given string object contains all numeric
    characters (0-9) returning True if it does and False if it
    doesn't.
    For example:
    "123456".IsNumeric()
    will return True whereas
    "abc123".IsNumeric()
    will return False.

  • IsOdd()

    Checks if the given number is odd.
    For example:
    234.IsOdd()
    will return False whereas
    339.IsOdd()
    will return True.

  • IsPrime()

    Checks if the given number is a prime number.
    For example:
    27.IsPrime()
    will return False whereas
    29.IsPrime()
    will return True.

  • IsStrong()

    Validates that the given string object contains a strong password string.
    For example:
    "abc123XYZ!@#".IsStrong()
    will return True whereas
    "abc123XYZ".IsStrong()
    will return False and
    "abc123XYZ".IsStrong(3)
    will return True and
    "abc123XYZ".IsStrong(2)
    will return True.
    The number parameter for IsStrong() indicates the number of criteria
    that has to be true before the string is considered strong. Valid
    values are 1 through 4 with the default value being 4.

  • IsUpper()

    Validates that the given string object contains only upper case letters.
    For example:
    "IsUpper test".IsUpper()
    will return False while
    "ISUPPER TEST".IsUpper()
    will return True and
    "ISUPPER TEST".IsUpper(false)
    will return False.

  • IsUrlRoot()

    Check if the given string object containing a URL, is that of the
    URL root only. Returns True if so, False if not.
    For example:
    "https://blog.cjvandyk.com".IsUrlRootOnly()
    will return True whereas
    "https://blog.cjvandyk.com/sites/Approval".IsUrlRootOnly()
    will return False.

  • IsVowel()

    Checks if the given char/string is an English vowel.
    This allows the developer the ability to check a string without
    having to first convert to a char e.g. as a substring return.
    For example:
    "test".Substring(2, 1).IsVowel()
    will return False since the "s" is checked whereas
    "test".Substring(1, 1).IsVowel()
    will return True since the "e" is checked.

  • IsZipCode()

    Checks if the given string object is in the valid format
    of a United States zip code i.e. nnnnn-nnnn or just nnnnn.
    For example:
    "12345-6789".IsZipCode()
    will return True whereas
    "1234-56789".IsZipCode()
    will return False.
    "12345".IsZipCode()
    will return True.

    "123456".IsZipCode()
    will return False.
    "1234".IsZipCode()
    will return False.

  • Left()

    This method returns text to the left of the index string. Use negative values for occurrence if the occurrence count should start from the end instead of its default from the beginning of the string.

  • Lines()

    This method returns the number of lines/sentences in the given string
    object.

  • Load()

    Language extension providing a universal method to all objects
    that allows them to be deserialized from disk.
    Does NOT require the [Serializable] property on object.
    For example:
    ComplexClass myClass = new ComplexClass();
    myClass = myClass.Load("My file path");
    Use .Save() to save objects to disk.

  • LoremIpsum()

    Poplates the given string with a given number of paragraphs of dummy
    text in the lorem ipsum style. For example:
    "".LoremIpsum(2)
    would yield
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer
    aliquam arcu rhoncus erat consectetur, quis rutrum augue tincidunt.
    Suspendisse elit ipsum, lobortis lobortis tellus eu, vulputate
    fringilla lorem. Cras molestie nibh sed turpis dapibus sollicitudin
    ut a nulla. Suspendisse blandit suscipit egestas. Nunc et ante mattis
    nulla vehicula rhoncus. Vivamus commodo nunc id ultricies accumsan.
    Mauris vitae ante ut justo venenatis tempus.

    Nunc posuere, nisi eu convallis convallis, quam urna sagittis ipsum,
    et tempor ante libero ac ex. Aenean lacus mi, blandit non eros luctus,
    ultrices consectetur nunc. Vivamus suscipit justo odio, a porta massa
    posuere ac. Aenean varius leo non ipsum porttitor eleifend. Phasellus
    accumsan ultrices massa et finibus. Nunc vestibulum augue ut bibendum
    facilisis. Donec est massa, lobortis quis molestie at, placerat a
    neque. Donec quis bibendum leo. Pellentesque ultricies ac odio id
    pharetra. Nulla enim massa, lacinia nec nunc nec, egestas pulvinar
    odio. Sed pulvinar molestie justo, eu hendrerit nunc blandit eu.
    Suspendisse et sapien quis ipsum scelerisque rutrum."

  • Match()

    Checks if the current string matches a given search mask.
    It ignores duplicate '' in the mask. '' is matched against
    0 or more characters. Duplicate '?' is treated as requiring
    the number of characters. '?' is matched against 1 or more
    characters.
    For example:
    "abcdefgh".Match("***a?c*")
    will return True while
    "abcdefgh".Match("***ac*")
    will return False but
    "abcdefgh".Match("?a?c*")
    will also return False because the first '?' requires a character
    before 'a'.

  • MorseCodeBeep()

    Takes a given System.String representing Morse code and audiblize
    it according to standards.
    https://www.infoplease.com/encyclopedia/science/engineering/electrical/morse-code
    Assumes the input value to be in Morse code format already.
    Use .ToMorseCode() to pre-convert text if needed.

  • NewCustomGuid()

    Returns a custom GUID starting with a custom string.
    For example:
    Extensions.NewCustomGuid("012")
    will return a new GUID that starts with "012".

  • Print()

    Print the byte[] to console, separated by spaces and space padded
    on the right to allow proper alignment for debug/testing output.
    For example:
    byte[] bytes = System.Text.Encoding.UTF8.GetBytes("blog.cjvandyk.com rocks!");
    bytes.Print();

  • printf()

    Simple printf method for console output with color control. Both
    text color and background color is returned to previous state
    after the string has been written to console.
    For example:
    printf("Hello World!", ConsoleColor.Red, ConsoleColor.White);
    will output the string to console in red text on a white background.

  • QueryStringToDictionary()

    Converts the current string (QueryString) to a dictionary of string
    objects for example:
    "?id=123&url=blog.cjvandyk.com&rating=awesome".QueryStringToDictionary()
    will return a dictionary with 3 entries thus:
    ["id"] = "123"
    ["url"] = "blog.cjvandyk.com"
    ["rating"] = "awesome"

  • QueryStringToNameValueCollection()

    Converts the current string (QueryString) to a NameValueCollection
    a List of KeyValue pairs. The main difference from .QueryStringToDictionary()
    is that duplicates can be contained in the list for example:
    "?id=123&url=blog.cjvandyk.com&id=789".QueryStringToNameValueCollection()
    will return a dictionary with 3 entries thus:
    ["id"] = "123"
    ["url"] = "blog.cjvandyk.com"
    ["id"] = "789"

  • Quote()

    Return the given string encased in requested quotes.
    Default is Constants.QuoteType.Double.
    For example:
    printf("https://blog.cjvandyk.com/sites/Rocks");
    printf("https://blog.cjvandyk.com/sites/Rocks").Quote();
    printf("https://blog.cjvandyk.com/sites/Rocks".Quote(
    Constants.QuoteType.Single));
    printf("https://blog.cjvandyk.com/sites/Rocks".Quote(
    Constants.QuoteType.Double));
    will return
    https://blog.cjvandyk.com/sites/Rocks
    "https://blog.cjvandyk.com/sites/Rocks"
    'https://blog.cjvandyk.com/sites/Rocks'
    "https://blog.cjvandyk.com/sites/Rocks"

  • RemoveExtraSpace()

    Trims leading and trailing white space and then removes all extra
    white space in the given string returning a single spaced result.
    For example:
    " blog.cjvandyk.com rocks ! ".RemoveExtraSpace()
    will return
    "blog.cjvandyk.com rocks !"

  • ReplaceTokens()

    Takes a given string object and replaces 1 to n tokens in the string
    with replacement tokens as defined in the given Dictionary of strings.

  • Retry()

    Checks if a System.Net.WebException contains a "Retry-After" header.
    If it does, it sleeps the thread for that period (+ 60 seconds)
    before reattempting the HTTP call that caused the exception in the
    first place. If no "Retry-After" header exist, the exception is
    simply rethrown.
    For example:
    System.Net.HttpWebRequest request ...
    Try
    {
    request.GetResponse();
    }
    Catch (System.Net.WebException ex)
    {
    ex.Retry(request);
    }

  • Save()

    Language extension providing a universal method to all objects
    that allows them to be serialized to disk.
    Does NOT require the [Serializable] property on object.
    For example:
    ComplexClass myClass = new ComplexClass(...<constructor parms>...);
    myClass.Save("My file path");
    Use .Load() to reload objects back from disk.

  • Set()

    _Language extension for properties. Use to set the value of the
    extension property in question.
    For example:
    Microsoft.SharePoint.Client client = new Microsoft.SharePoint.Client("https://blog.cjvandyk.com");
    client.ExecutingWebRequest += ClientContext_ExecutingWebRequest;
    client.Set("HeaderDecoration", "NONISV|Crayveon|MyApp/1.0");
    This allows the creation of the extension property "HeaderDecoration"
    which can be changed as needed. Later in the delegate method we
    refer back to the extension property value thus:
    private void ClientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
    {
    e.WebRequestExecutor.WebRequest.UserAgent = (string)e.Get("HeaderDecoration");
    }
    NOTE: We did not have to access the ClientContext class in order to
    retrieve the "HeaderDecoration" value since the extension was
    done against the System.Object class. As such, any object can
    be used to retrieve the extension property value, as long as
    you know the key value under which the property was stored and
    you know the type to which the returned value needs to be cast.
    A derived override method for Get() and Set() can be defined
    using specific class objects if finer controls is needed.

  • SingleQuote()

    Return the given string encased in single quotes.
    For example:
    printf("https://blog.cjvandyk.com/sites/Rocks");
    printf("https://blog.cjvandyk.com/sites/Rocks".SingleQuote());
    will return
    https://blog.cjvandyk.com/sites/Rocks
    'https://blog.cjvandyk.com/sites/Rocks'

  • Singularize()

    Parses the given string removing multiples of a given character.
    For example:
    string searchMask = "***??abc*";
    searchMask.Singularize('*')
    will return
    "*??abc*"

  • Smallest()

    Return the smallest of two given values.
    For example:
    Smallest(23, 31)
    will return
    23

  • Substring()

    Extends the .Substring(startIndex) and .Substring(startIndex, length)
    methods to the System.Text.StringBuilder class.
    For example:
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("abc1abc2abc3abc4");
    sb.Substring(5);
    will return bc2abc3abc4
    sb.Substring(5, 3);
    will return bc2
    Adds the FromHead/FromTail overloaded methods.
    FromHead returns the "length" of characters from the head of the given
    string.
    For example:
    sb.Substring(3, Constants.SubstringType.FromHead);
    sb.Substring(5, Constants.SubstringType.FromHead);
    sb.Substring(8, Constants.SubstringType.FromHead);
    will return
    abc
    abc1a
    abc1abc2
    FromTail returns the "length" of characters from the tail of the given
    string.
    For example:
    sb.Substring(3, Constants.SubstringType.FromTail);
    sb.Substring(5, Constants.SubstringType.FromTail);
    sb.Substring(8, Constants.SubstringType.FromTail);
    will return
    bc4
    3abc4
    abc3abc4
    Adds the LeftOfIndex/RightOfIndex overloaded methods.
    LeftOfIndex returns the "length" of characters to the LEFT of the
    located index representing the "occurence"th match of the "index"
    string.
    For example:
    sb.Substring(5, "abc", Constants.SubstringType.LeftOfIndex, 0);
    sb.Substring(5, "abc", Constants.SubstringType.LeftOfIndex, 1);
    sb.Substring(5, "abc", Constants.SubstringType.LeftOfIndex, 2);
    sb.Substring(5, "abc", Constants.SubstringType.LeftOfIndex, 3);
    sb.Substring(5, "abc", Constants.SubstringType.LeftOfIndex, 4);
    will return
    <br>
    abc1
    1abc2
    2abc3
    RightOfIndex returns the "length" of characters to the RIGHT of the
    located index representing the "occurence"th match of the "index"
    string.
    For example:
    sb.Substring(5, "abc", Constants.SubstringType.RigthOfIndex, 0);
    sb.Substring(5, "abc", Constants.SubstringType.RigthOfIndex, 1);
    sb.Substring(5, "abc", Constants.SubstringType.RigthOfIndex, 2);
    sb.Substring(5, "abc", Constants.SubstringType.RigthOfIndex, 3);
    sb.Substring(5, "abc", Constants.SubstringType.RigthOfIndex, 4);
    will return
    ``
    1abc2
    `2abc3`
    `3abc4`
    `4`

  • System.Timer class

    This class provides and easy way to time things like a stopwatch.
    .Start() starts the timer.
    .Stop() stops the timer.
    .Pause() pauses the timer.
    .Resume() resumes the timer.
    .Reset() resets the timer.
    For example:
    System.Timer timer = new System.Timer();
    timer.Start();
    <DO STUFF>
    System.TimeSpan howlong = timer.Stop();

  • TimeStamp()

    Returns a string representing the current local date time stamp to
    either just the day or down to the millisecond. Used for creating
    unique log file names.
    For example:
    TimeStamp()
    will return
    [email protected]
    whereas
    TimeStamp(true)
    will return
    2021-03-01

  • ToBinary()

    This method returns the given string represented in 1s and 0s as
    a binary result.
    For example:
    "This test".ToBinary()
    will return
    1010100 1101000 1101001 1110011 100000 1110100 1100101 1110011 1110100

  • ToEnum()

    This method matches a given string to the given enum set and returns
    the matched enum.
    For example:
    enum testEnum { first, second, third };
    var testEnumResult = "first".ToEnum<testEnum>();
    Console.WriteLine(testEnumResult == testEnum.first);
    will return
    True

  • ToMorseCode()

    Convert given string to its Morse code representation.
    Undefined characters will return in the format:
    Undefined:[char=""]
    For example:
    "sos@".ToMorseCode()
    will return
    "...---...<Undefined:[@]>"

  • ToQueryString()

    Convert given Dictionary<string, string> into a querystring.
    For example:
    Dictionary<string, string> dic1 = new Dictionary<string, string>();
    dic1.Add("Parm1", "Val1");
    dic1.Add("Parm2", "Val2");
    dic1.Add("Parm3", "Val3");
    Console.WriteLine(dic1.ToQueryString());
    will return
    "?Parm1=Val1&Parm2=Val2&Parm3=Val3"

  • Binary Data Size Convertions

    • System.Double.ToNumberBytes() >>> Returns the given number expressed as Bytes.
    • System.Double.ToKB() >>> Returns the given number expressed as Kilobytes (2^10).
    • System.Double.ToMB() >>> Returns the given number expressed as Megabytes (2^20).
    • System.Double.ToGB() >>> Returns the given number expressed as Gigabytes (2^30).
    • System.Double.ToTB() >>> Returns the given number expressed as Terrabytes (2^40).
    • System.Double.ToPB() >>> Returns the given number expressed as Petabytes (2^50).
    • System.Double.ToEB() >>> Returns the given number expressed as Exabytes (2^60).
    • System.Double.ToZB() >>> Returns the given number expressed as Zettabytes (2^70).
    • System.Double.ToYB() >>> Returns the given number expressed as Yottabytes (2^80).
    • System.Double.ToBB() >>> Returns the given number expressed as Brontobytes (2^90).
    • System.Double.ToGpB() >>> Returns the given number expressed as Geopbytes (2^100).
    • System.Double.ToSB() >>> Returns the given number expressed as Saganbytes (2^110).
    • System.Double.ToPaB() >>> Returns the given number expressed as Pijabytes (2^120).
    • System.Double.ToAB() >>> Returns the given number expressed as Alphabytes (2^130).
    • System.Double.ToPlB() >>> Returns the given number expressed as Pectrolbytes (2^140).
    • System.Double.ToBrB() >>> Returns the given number expressed as Bolgerbytes (2^150).
    • System.Double.ToSoB() >>> Returns the given number expressed as Sambobytes (2^160).
    • System.Double.ToQB() >>> Returns the given number expressed as Quesabytes (2^170).
    • System.Double.ToKaB() >>> Returns the given number expressed as Kinsabytes (2^180).
    • System.Double.ToRB() >>> Returns the given number expressed as Rutherbytes (2^190).
    • System.Double.ToDB() >>> Returns the given number expressed as Dubnibytes (2^200).
    • System.Double.ToHB() >>> Returns the given number expressed as Hassiubytes (2^210).
    • System.Double.ToMrB() >>> Returns the given number expressed as Meitnerbytes (2^220).
    • System.Double.ToDdB() >>> Returns the given number expressed as Darmstadbytes (2^230).
    • System.Double.ToRtB() >>> Returns the given number expressed as Roentbytes (2^240).
    • System.Double.ToShB() >>> Returns the given number expressed as Sophobytes (2^250).
    • System.Double.ToCB() >>> Returns the given number expressed as Coperbytes (2^260).
    • System.Double.ToKkB() >>> Returns the given number expressed as Koentekbytes (2^270).

    For example:
    double dbl = 1;
    Console.WriteLine(dbl.ToKB(Constants.NumberType.TB));
    Console.WriteLine(dbl.ToKB(Constants.NumberType.GB));
    Console.WriteLine(dbl.ToKB(Constants.NumberType.ZB));
    will return
    1073741824
    1048576
    1.15292150460685E+18

  • ToTimeZone()

    Convert given DateTime between different time zones with ease.
    For example:
    System.DateTime now = System.DateTime.UtcNow;
    now.ToTimeZone(
    Constants.TimeZone.UTC,
    Constants.TimeZone.EasternStandardTime));
    will return the current UTC time as Eastern time.

  • TrimLength()

    Returns part of the given System.Text.StringBuilder object
    tuncated to the requested length minus the length of the
    suffix.
    If the string is null or empty, it returns said value.
    If the string is shorter than the requested length, it returns
    the whole string.
    For example:
    "The Extensions.cs NuGet package rocks!".TrimLength(20)
    will return "The Extensions.cs..." while
    "The Extensions.cs NuGet package rocks!".TrimLength(20, "")
    will return "The Extensions.cs Nu" and
    "The Extensions.cs NuGet package rocks!".TrimLength(20, ">>")
    will return "The Extensions.cs >>"

  • TrimStart()

    Trims a given string rather than just a character, from the start of
    the target string. The traditional Trim() only allowed char values
    to be trimmed. TrimStart() solves that limitation in an easier to
    fashion that using Substring().
    For example:
    "https://blog.cjvandyk.com".TrimStart("https://")
    will return
    "blog.cjvandyk.com"

  • Validate()

    Makes quick work of conducting multiple types of validations on all
    parameters. It takes a parameter array of ErrorType and conducts
    the appropriate validation such as null checking, non-zero checking
    etc. against the parameter array passed.
    For example:
    Validate(Constants.ErrorTypeAll, amount, percent, years, frequency);
    will perform all defined error checks against the amount, percent,
    years and frequency parameters.

    • Add Logging.ConstructMessage().
    • Add Logging.ConsoleMessage().
    • Add Logging.EventLogMessage().
    • Add Logging.FileMessage().
  • ValidateNoNulls()

    Makes quick work of null validating all parameters you pass to it.
    This method takes a variable number of parameters and validates that
    all parameters are not null. If a parameter is found to be null, a
    ArgumentNullException is thrown.
    For example:
    void MyMethod(string str, double dbl, MyClass cls)
    {
    Universal.ValidateNoNulls(str, dbl, cls);<br> ...Your code here...<br> }<br> You do not have to pass all parameters, but can instead do this:<br> void MyMethod(string str, double dbl, MyClass cls)<br> {<br> Universal.ValidateNoNulls(str, cls);
    ...Your code here...
    }
    where we chose NOT to validate the double dbl in this case.

  • Words()

    This method returns the number of words used in the given string
    object. For example:
    "This is my test".Words()
    will return 4 whereas
    "ThisIsMyTest".Words()
    will return 1.

  • Wrn()

    Write a Warning message to active channels (console, event log, file)
    using the System.Logging class.

Visitor Count

About

Extensions.dll contains extension methods that enhance existing C# classes thus making life easier for developers.

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages