diff --git a/Easy.Common.Tests.Unit/Enum/EnumTests.cs b/Easy.Common.Tests.Unit/Enum/EnumTests.cs index d46e032..451db0c 100644 --- a/Easy.Common.Tests.Unit/Enum/EnumTests.cs +++ b/Easy.Common.Tests.Unit/Enum/EnumTests.cs @@ -1,6 +1,7 @@ namespace Easy.Common.Tests.Unit.Enum; using System.Collections.Generic; +using System.Runtime.CompilerServices; using Easy.Common.Interfaces; using NUnit.Framework; using Shouldly; @@ -31,11 +32,17 @@ public void When_getting_enum_values() values.ShouldBe(new[] { MyEnum.OptionA, MyEnum.OptionB }, ignoreOrder: true); } + [Test] + public void When_calling_toString() + { + MyEnum.OptionB.ToString().ShouldBe("[1] OptionB"); + } + sealed record class MyEnum : Enum { - public static readonly MyEnum OptionA = new(0, nameof(OptionA)); - public static readonly MyEnum OptionB = new(1, nameof(OptionB)); + public static readonly MyEnum OptionA = new(0); + public static readonly MyEnum OptionB = new(1); - private MyEnum(int id, string name) : base(id, name) { } + private MyEnum(int id, [CallerMemberName] string name = default!) : base(id, name!) { } } } \ No newline at end of file diff --git a/Easy.Common/Easy.Common.csproj b/Easy.Common/Easy.Common.csproj index 02e32e7..b7b7b6d 100644 --- a/Easy.Common/Easy.Common.csproj +++ b/Easy.Common/Easy.Common.csproj @@ -3,7 +3,7 @@ Easy.Common A set of useful methods used across Easy.* projects. Nima Ara - 2023 Nima Ara + 2024 Nima Ara Easy Common Easy;Common https://github.com/NimaAra/Easy.Common diff --git a/Easy.Common/Enum.cs b/Easy.Common/Enum.cs index 700910d..ce9c4c4 100644 --- a/Easy.Common/Enum.cs +++ b/Easy.Common/Enum.cs @@ -1,14 +1,15 @@ namespace Easy.Common; +using Easy.Common.Interfaces; using System.Collections.Generic; using System.Linq; using System.Reflection; -using Easy.Common.Interfaces; +using System.Runtime.CompilerServices; /// /// An abstraction for representing a class based enum. /// -public abstract record class Enum(int Id, string Name) : IEnum where T : IEnum +public abstract record class Enum(int Id, [CallerMemberName] string Name = default!) : IEnum where T : IEnum { /// /// Retrieves a list of the values of the constants in a specified enumeration of type . @@ -18,4 +19,9 @@ public abstract record class Enum(int Id, string Name) : IEnum where T : IEnu .Select(f => f.GetValue(null)) .Cast() .ToList(); + + /// + /// Returns the textual representation of the enum. + /// + public sealed override string ToString() => $"[{Id}] {Name}"; } \ No newline at end of file diff --git a/Easy.Common/Interfaces/IEnum.cs b/Easy.Common/Interfaces/IEnum.cs index 09c769a..1926a0f 100644 --- a/Easy.Common/Interfaces/IEnum.cs +++ b/Easy.Common/Interfaces/IEnum.cs @@ -11,7 +11,7 @@ public interface IEnum int Id { get; } /// - /// Gets the name. + /// Gets the Name. /// string Name { get; } } \ No newline at end of file