According to the Directive Constraints on type parameters (C # Programming Guide) ), and I quote:
When applying class T restrictions: Avoid == and! = Operators in the type parameter, because these operators will be for reference identity only, not for equality of values. This is true even if these statements are overloaded in the type that is used as the argument. The following code illustrates this point; the output is false, even if the String class overloads the == operator.
In the following example:
public static void OpTest<T>(T s, T t) where T : class { System.Console.WriteLine(s == t); } static void Main() { string s1 = "target"; System.Text.StringBuilder sb = new System.Text.StringBuilder("target"); string s2 = sb.ToString(); OpTest<string>(s1, s2); }
However, ReSharper (I just started using the demo / trial version to find out if I should do this) gives a hint / hint for checking parameters like this:
public Node(T type, Index2D index2D, int f, Node<T> parent) { if (type == null) throw new ArgumentNullException("type"); if (index2D == null) throw new ArgumentNullException("index2D"); if (parent == null) throw new ArgumentNullException("parent"); }
(T is limited where T : class, new() )
Can I safely follow ReSharper without encountering problems that the C # documentation is trying to explain to me?
source share