NStandard icon indicating copy to clipboard operation
NStandard copied to clipboard

DotNet Core extensions for system library.

NStandard

.NET extension library for system library.

No depndencies, providing compatible implementations for .NET Framework similar to some of .NET Core functions.


You can use it to get the following support:

  • Implementation of new feature compatibility for .NET Framework.
  • Common data structures and some code patterns.

These frameworks are supported:

NET NET Standard NET Framework
- .NET 5.0
- .NET 6.0
- .NET Standard 2.0
- .NET Standard 2.1
- .NET Framework 3.5
- .NET Framework 4.0
- .NET Framework 4.5
- .NET Framework 4.5.1
- .NET Framework 4.5.2
- .NET Framework 4.6

Install

  • Package Manager

    Install-Package NStandard
    
  • .NET CLI

    dotnet add package NStandard
    
  • PackageReference

    <PackageReference Include="NStandard" Version="*" />
    

Recently

Version: 0.14.0

  • Add Any.Zip function instead of Zipper.Create.

Version: 0.13.0

  • BREAKING CHANGE: To make the name more readable, the extension method class name has been renamed from X... to ...Extensions.

Version: 0.8.40

  • Add struct HashCode for .NET Framework, which behaves like .NET Core.
  • Adjust precompiled macros to make them more reasonable.

About the Official Version (v1.0.0, Not Release)

Functions:

Extensions:

Currently, the following functions still need to be improved:


Chaining Extension Functions

  • Then

    Run a task for an object, then return itself.

    public class AppSecurity
    {
        public Aes Aes = Aes.Create();
        public AppSecurity()
        {
            Aes.Key = "1234567890123456".Bytes();
        }
    }
    

    Simplify:

    public class AppSecurity
    {
        public Aes Aes = Aes.Create().Then(x => x.Key = "1234567890123456".Bytes());
    }
    
  • For

    Casts the object to another object through the specified convert method.

    var orderYear = Product.Order.Year;
    var year = orderYear > 2020 ? orderYear : 2020;
    

    Simplify:

    var year = Product.Order.Year.For(y => y > 2020 ? y : 2020);
    
  • Let

    Use a method to initialize each element of an array.

    var arr = new int[5];
    for (int i = 0; i < arr.Length; i++)
    {
        arr[i] = i * 2 + 1;
    }
    

    Simplify:

    var arr = new int[5].Let(i => i * 2 + 1);
    

Core DS Extensions

Extension: XDateTime

  • StartOf / EndOf

    var today = new DateTime(2012, 4, 16, 22, 23, 24);
    today.StartOfYear();        // 2012/1/1 0:00:00
    today.StartOfMonth();       // 2012/4/1 0:00:00
    today.StartOfDay();         // 2012/4/16 0:00:00
    today.EndOfYear();          // 2012/12/31 23:59:59
    today.EndOfMonth();         // 2012/4/30 23:59:59
    today.EndOfDay();           // 2012/4/16 23:59:59
    
  • UnixTime

    var dt = new DateTime(1970, 1, 1, 16, 0, 0, DateTimeKind.Utc);
    dt.UnixTimeSeconds();           // 57600
    dt.UnixTimeMilliseconds();      // 57600000
    
  • And more ...

Static Extension: DateTimeEx

  • YearDiff / ExactYearDiff

    The number of complete years in the period, similar as *DATEDIF(*, , "Y") function in Excel.

    DateTimeEx.YearDiff(
        new DateTime(2000, 2, 29),
        new DateTime(2001, 2, 28));      // 0
    
    DateTimeEx.ExactYearDiff(
        new DateTime(2000, 2, 29),       // 365 / 366
        new DateTime(2001, 2, 28));      // 0.9972677595628415
    
  • MonthDiff / ExactMonthDiff

    The number of complete months in the period, similar as *DATEDIF(*, , "M") function in Excel.

    DateTimeEx.MonthDiff(
        new DateTime(2000, 2, 29),
        new DateTime(2001, 2, 28));      // 11
    
    DateTimeEx.ExactMonthDiff(
        new DateTime(2000, 2, 29),       // 11 + (2 + 28) / 31
        new DateTime(2001, 2, 28));      // 11.967741935483872
    
  • And more ...