I stumbled upon an extension method that applies to structs (SomeStruct) and returns a value equal to the default(SomeStruct) value default(SomeStruct) (when the constructor without parameters is called).
public static bool IsDefault<T> (this T value) where T : struct { return (!EqualityComparer<T>.Default.Equals(value, default(T))); }
This made me wonder if the structure was built. This is purely out of curiosity, as there are pros and cons to boxing / passing by value depending on the context.
Assumptions:
- The first of the following methods is illegal because structures do not implicitly override the equality operators
==/!= . - The second “appears” to avoid boxing.
- The third method should always insert the structure as it calls
object.Equals(object o) . - The fourth one has both overloads available
(object/T) , so I guess this will also avoid boxing. However, the target structure needs to implement the IEquatable<T> interface, which makes the helper extension method not very useful.
Options:
public static bool IsDefault<T> (this T value) where T : struct { // Illegal since there is no way to know whether T implements the ==/!= operators. return (value == default(T)); } public static bool IsDefault<T> (this T value) where T : struct { return (!EqualityComparer<T>.Default.Equals(value, default(T))); } public static bool IsDefault<T> (this T value) where T : struct { return (value.Equals(default(T))); } public static bool IsDefault<T> (this T value) where T : struct, IEquatable<T> { return (value.Equals(default(T))); }
This question concerns the confirmation of the above assumptions and, if I do not understand and / or something I do not understand.
source share