воскресенье, 24 октября 2021 г.

TypeName, Method, Reflection

https://stackoverflow.com/questions/2113069/c-sharp-getting-its-own-class-name

this.GetType().Name
protected string GetThisClassName() { return this.GetType().Name;

public static class Extension
{
    public static string NameOf(this object o)
    {
        return o.GetType().Name;
    }
}

And then use like this:

public class MyProgram
{
    string thisClassName;

    public MyProgram()
    {
        this.thisClassName = this.NameOf();
    }
}
string CurrentClass = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name.ToString();
string CurrentClass = System.Reflection.MethodBase.GetCurrentMethod().Name.ToString();






пятница, 22 октября 2021 г.

Inline, Function, Macro, Preprocessor

https://stackoverflow.com/questions/709463/c-sharp-macro-definitions-in-preprocessor

There is no direct equivalent to C-style macros in C#, but inlined static methods - with or without #if/#elseif/#else pragmas - is the closest you can get:

 /// <summary>
        /// Prints a message when in debug mode
        /// </summary>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static unsafe void Log(object message) {
#if DEBUG
            Console.WriteLine(message);
#endif
        }

        /// <summary>
        /// Prints a formatted message when in debug mode
        /// </summary>
        /// <param name="format">A composite format string</param>
        /// <param name="args">An array of objects to write using format</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static unsafe void Log(string format, params object[] args) {
#if DEBUG
            Console.WriteLine(format, args);
#endif
        }

        /// <summary>
        /// Computes the square of a number
        /// </summary>
        /// <param name="x">The value</param>
        /// <returns>x * x</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static double Square(double x) {
            return x * x;
        }

        /// <summary>
        /// Wipes a region of memory
        /// </summary>
        /// <param name="buffer">The buffer</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static unsafe void ClearBuffer(ref byte[] buffer) {
            ClearBuffer(ref buffer, 0, buffer.Length);
        }

        /// <summary>
        /// Wipes a region of memory
        /// </summary>
        /// <param name="buffer">The buffer</param>
        /// <param name="offset">Start index</param>
        /// <param name="length">Number of bytes to clear</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static unsafe void ClearBuffer(ref byte[] buffer, int offset, int length) {
            fixed(byte* ptrBuffer = &buffer[offset]) {
                for(int i = 0; i < length; ++i) {
                    *(ptrBuffer + i) = 0;
                }
            }
        }

GC.Collector, Clear, Array

https://stackoverflow.com/questions/20859373/clear-array-after-is-used-c-sharp

четверг, 21 октября 2021 г.

CircularArray, C#, Enumerator

https://www.geeksforgeeks.org/circular-array/







np.arrange, linspace, math.net

https://stackoverflow.com/questions/68293706/implementing-linspace-from-python-to-c-sharp-to-get-a-vector

Vector<double>.Build.Dense(bands, i => -1 + (dx / 2) + i++ * dx);

Xu, Books

https://drxudotnet.com/

Linq, New, Methods

https://t.me/csharpproglib/2076

Chunk, MaxBy, MinBy

C#, Books

https://t.me/csharpproglib/2075


ML.NET

https://t.me/csharpproglib/2086

https://devblogs.microsoft.com/dotnet/ml-net-and-model-builder-october-updates/


Templates, Project, Github

GitHub - Dotnet-Boxed/Templates: .NET project templates with batteries included.

https://t.me/csharpproglib/2097

https://github.com/Dotnet-Boxed/Templates


Data, WebApi, InMemory,

Community Question: Working With Data In An HTTP API

https://t.me/csharpproglib/2099

https://khalidabuhakmeh.com/community-question-working-with-data-in-an-http-api


Paging, EntityFramework, ASP.NETCore

Cursor Paging With Entity Framework Core and ASP.NET Core

https://t.me/csharpproglib/2110

https://khalidabuhakmeh.com/cursor-paging-with-entity-framework-core-and-aspnet-core

воскресенье, 17 октября 2021 г.

Compress Strings With .NET and C#, Encoding, Compress

https://proglib.io/w/52991f4e

https://khalidabuhakmeh.com/compress-strings-with-dotnet-and-csharp

var value = "hello world";
var level = CompressionLevel.Fastest;

var bytes = Encoding.Unicode.GetBytes(value);
await using var input = new MemoryStream(bytes);
await using var output = new MemoryStream();

// GZipStream with BrotliStream
await using var stream = new GZipStream(output, level);

await input.CopyToAsync(stream);

var result = output.ToArray();
var resultString = Convert.ToBase64String(result);
public static class Compression
{
    public static async Task<CompressionResult> ToGzipAsync(this string value, CompressionLevel level = CompressionLevel.Fastest)
    {
        var bytes = Encoding.Unicode.GetBytes(value);
        await using var input = new MemoryStream(bytes);
        await using var output = new MemoryStream();
        await using var stream = new GZipStream(output, level);

        await input.CopyToAsync(stream);
        
        var result = output.ToArray();

        return new CompressionResult(
            new CompressionValue(value, bytes.Length),
            new CompressionValue(Convert.ToBase64String(result), result.Length),
            level,
            "Gzip");
    }
    
    public static async Task<CompressionResult> ToBrotliAsync(this string value, CompressionLevel level = CompressionLevel.Fastest)
    {
        var bytes = Encoding.Unicode.GetBytes(value);
        await using var input = new MemoryStream(bytes);
        await using var output = new MemoryStream();
        await using var stream = new BrotliStream(output, level);

        await input.CopyToAsync(stream);
        await stream.FlushAsync();
        
        var result = output.ToArray();

        return new CompressionResult(
            new CompressionValue(value, bytes.Length),
            new CompressionValue(Convert.ToBase64String(result), result.Length),
            level,
            "Brotli"
        );
    }

    public static async Task<string> FromGzipAsync(this string value)
    {
        var bytes = Convert.FromBase64String(value);
        await using var input = new MemoryStream(bytes);
        await using var output = new MemoryStream();
        await using var stream = new GZipStream(input, CompressionMode.Decompress);

        await stream.CopyToAsync(output);
        await stream.FlushAsync();
        
        return Encoding.Unicode.GetString(output.ToArray());
    }

    public static async Task<string> FromBrotliAsync(this string value)
    {
        var bytes = Convert.FromBase64String(value);
        await using var input = new MemoryStream(bytes);
        await using var output = new MemoryStream();
        await using var stream = new BrotliStream(input, CompressionMode.Decompress);

        await stream.CopyToAsync(output);
        
        return Encoding.Unicode.GetString(output.ToArray());
    }
}

public record CompressionResult(
    CompressionValue Original,
    CompressionValue Result,
    CompressionLevel Level,
    string Kind
)
{
    public int Difference =>
        Original.Size - Result.Size;

    public decimal Percent =>
      Math.Abs(Difference / (decimal) Original.Size);
}

public record CompressionValue(
    string Value,
    int Size
);
var comedyOfErrors = await File.ReadAllTextAsync("the-comedy-of-errors.txt");

var compressions = new[]
{
    await comedyOfErrors.ToGzipAsync(),
    await comedyOfErrors.ToBrotliAsync()
};

var table = new Table()
    .MarkdownBorder()
    .Title("compression in bytes")
    .ShowHeaders()
    .AddColumns("kind", "level", "before", "after", "difference", "% reduction");

foreach (var result in compressions)
{
    table
        .AddRow(
            result.Kind,
            result.Level.ToString(),
            result.Original.Size.ToString("N0"),
            result.Result.Size.ToString("N0"),
            result.Difference.ToString("N0"),
            result.Percent.ToString("P")
        );
}

AnsiConsole.Render(table);