This is a simple task, yet one which is frequently carried out incorrectly. This kind of comparison is often required when dealing with classes that look like the following.
Now the knee-jerk reaction to compare
value in this case is to do this.
However, this doesn’t compile. This makes sense, because we cannot guarantee that the type
T overloads the equality operator (reference types do by default, but value types do not). In light of this problem, many people will resort to the following.
However this poses a different problem. If
_variable is ever null, your program will throw a null reference exception. But there is a way around this. Simply use the static implementation of
System.Object as follows.