Skip to content

WirthLukas/KotlinSharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

KotlinSharp

This Library is a C# implementation of some of the nice Functions from the Kotlin Standard Library. I used the .Net Core 3.1 Framework with nullable types (article in msdn-magazine).

Scope Functions

The Extension Functions of a generic type are implemented in the GeneralExtensions class

All standalone functions are in the "Kotlin" class named K

Examples

You can find all of these examples and more in the ConsoleTests Project.

Singleton

Normally a implementation of a Singleton

public class Repository
{
    private static Repository? _instance = null;
    private static readonly object Locker = new object();

    // Normally a Instance Property
    public static Repository Instance
    {
        get 
        {
            if (_instance == null)
            {
                lock(Locker)
                {
                    if (_instance == null)
                        _instance = Build();
                }
            }

            return _instance;
        }
    }

    // Example Build Method
    private static Repository Build() => new Repository();

    private Repository()
    {}
}

Implementation in Kotlin Style

public static Repository Instance
    => _instance ?? K.Synchronized(Locker, 
        () => _instance ?? Build().Also(it => _instance = it));

Implementation with C# 8 ??= Operator

public static Repository InstanceX 
    => _instance ?? K.Synchronized(Locker, () => _instance ??= Build());

Example Console Program

class Program
{
    static void Main(string[] args)
    {
        string? test = null;
        Console.WriteLine($"Nullable: {test.ToStringK()}");     // Writes "Nullable: null"; raises no Exception!
        test = "with a value";
        Console.WriteLine($"Nullable: {test.ToStringK()}");     // Writes "Nullable: with a value"

        /*long length = new FileStream("test.txt", FileMode.OpenOrCreate).Use(fs =>
        {
            Console.WriteLine($"File: {fs.Name}");
            return fs.Length;
        });*/
        
        // Repository r = Repository.Instance;
        // Console.WriteLine($"Repository: {r.ToStringK()}");

        Repository.Instance.Let(it => Console.WriteLine($"Repository: {it.ToStringK()}"));
        
        var aVeryLongNameForRepository = Repository.Instance;
        K.With(aVeryLongNameForRepository, (Repository it) =>
        {
            it.Val1 = 1;
            it.Val2 = 2;
            it.Val3 = 3;
        });

        Repository? bsp = K.With(Repository.Instance, it =>
        {
            it.Val1 = 1;
            it.Val2 = 2;
            it.Val3 = 3;
            return it;
        }).TakeIf(it => it.Val1 == 1);
    }
}

About

C# implementation of some nice Kotlin Methods

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages