Skip to content

Informatievlaanderen/tostring-builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Be.Vlaanderen.Basisregisters.Utilities.ToStringBuilder Build Status

Goal

Easily concatenate properties of an object into a string.

Usage

As a static helper

The most basic form of this library is to simply call it passing in the strings to concatenate.

By default it will concatenate them with a ,  between them.

namespace Example
{
    using System;
    using Be.Vlaanderen.Basisregisters.Utilities;

    public class Test
    {
        public int Number { get; set; }
        public bool IsTrue { get; set; }

        public override string ToString()
            => ToStringBuilder.ToString(new object[] { Number, IsTrue });
    }

    public class Program
    {
        public static void Main(string[] _)
        {
            var test = new Test { Number = 42, IsTrue = false };
            Console.WriteLine(test);
        }
    }
}

The result when running this:

$ dotnet run
42, False

Using yield return

You can also use this to pass in an IEnumerable which allows you to use yield return to supply the values for the concatenation.

This allows you to stream values using yield.

namespace Example
{
    using System;
    using System.Collections.Generic;
    using Be.Vlaanderen.Basisregisters.Utilities;

    public class Test
    {
        public int Number { get; set; }
        public bool IsTrue { get; set; }

        public override string ToString()
            => ToStringBuilder.ToString(Fields);

        private IEnumerable<object> Fields()
        {
            yield return Number;
            yield return IsTrue;
        }
    }

    public class Program
    {
        public static void Main(string[] _)
        {
            var test = new Test { Number = 42, IsTrue = true };
            Console.WriteLine(test);
        }
    }
}

The result when running this:

$ dotnet run
42, True

As an extension method

There is also an extension method on T. Additionally, in all forms you can pass in the method used to concatenate the string.

namespace Example
{
    using System;
    using Be.Vlaanderen.Basisregisters.Utilities;

    public class Test
    {
        public int Number { get; set; }
        public bool IsTrue { get; set; }
    }

    public class Dummy
    {
        public void Example(Test test)
        {
            var s = test.ToString(
                x => new object[] {x.Number, x.IsTrue},
                (s1, s2) => $"{s1}/{s2}");

            Console.WriteLine(s);
        }
    }

    public class Program
    {
        public static void Main(string[] _)
        {
            var test = new Test { Number = 42, IsTrue = true };
            var dummy = new Dummy();

            dummy.Example(test);
        }
    }
}

The result when running this:

$ dotnet run
42/True

License

MIT License

Credits

Languages & Frameworks

  • .NET Core - MIT
  • .NET Core Runtime - CoreCLR is the runtime for .NET Core. It includes the garbage collector, JIT compiler, primitive data types and low-level classes. - MIT
  • .NET Core APIs - CoreFX is the foundational class libraries for .NET Core. It includes types for collections, file systems, console, JSON, XML, async and many others. - MIT
  • .NET Core SDK - Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI. - MIT
  • Roslyn and C# - The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs. - Apache License 2.0
  • F# - The F# Compiler, Core Library & Tools - MIT
  • F# and .NET Core - F# and .NET Core SDK working together. - MIT

Libraries

  • Paket - A dependency manager for .NET with support for NuGet packages and Git repositories. - MIT
  • FAKE - "FAKE - F# Make" is a cross platform build automation system. - MIT
  • xUnit - xUnit.net is a free, open source, community-focused unit testing tool for the .NET Framework. - Apache License 2.0
  • FluentAssertions - Fluent API for asserting the results of unit tests. - Apache License 2.0

Tooling

Flemish Government Libraries