内容目录
第一种:
private bool IsDefaultValue(T value)
{
Span<byte> valueBytes = MemoryMarshal.AsBytes<T>(new T []{ value }.AsSpan());
var defaultBytes = new byte[valueBytes.Length];
return valueBytes.SequenceEqual(defaultBytes);
}
第二种:
public class Test<T> where T : struct, IComparable
{
public T Id { get; private set; }
public Test(T id)
{
Id = id;
}
public bool IsDefault()
{
return Id.CompareTo(0) == 0;
}
}
第三种:
public class Test<T> where T : struct, IComparable<T>
{
private readonly static T defaultValue;
static Test()
{
defaultValue = new T();
}
public T Id { get; private set; }
public Test(T id)
{
Id = id;
}
public bool IsDefault()
{
return Id.CompareTo(defaultValue) == 0;
}
文章评论