Innovenergy_trunk/csharp/Lib/Utils/NullableUtilsStruct.Struct.cs

24 lines
669 B
C#
Raw Normal View History

using System.Diagnostics.CodeAnalysis;
namespace InnovEnergy.Lib.Utils;
public static class NullableUtilsStruct
{
public static IEnumerable<T> ToEnumerable<T>(this T? t) where T : struct
{
if (t.HasValue)
yield return t.Value;
}
public static T ThrowIfNull<T>(this T? t) where T : struct => ThrowIfNull(t, null);
public static T ThrowIfNull<T>(this T? t, String? message) where T : struct
{
if (!t.HasValue)
throw new NullReferenceException(message);
return t.Value;
}
public static Boolean IsNull<T>([NotNullWhen(returnValue: false)] this T? t) where T : struct => t.HasValue;
}