c# - When Implementing IEqualityComparer Should GetHashCode check for null? -


when implementing iequalitycomparer<product> (product class), resharper complains null check below false:

public int gethashcode(product product) {   // check whether object null.    if (object.referenceequals(product, null))     return 0;    // ... other stuff ... } 

(code example msdn vs.9 documentation of enumerable.except)

resharper may wrong, when searching answer, came across official documentation iequalitycomparer<t> has example null not checked for:

public int gethashcode(box bx) {     int hcode = bx.height ^ bx.length ^ bx.width;     return hcode.gethashcode(); } 

additionally, documentation gethashcode() states argumentnullexception thrown when "the type of obj reference type , obj null."

so, when implementing iequalitycomparer should gethashcode check null, , if so, should null (throw exception or return value)?

i'm interested in .net framework official documentation specifies 1 way or if null should checked.

resharper wrong.

obviously code write can call particular gethashcode method , pass in null value. known methods might ensure never happen, resharper can take existing code (patterns) account.

so in case, check null , "right thing".


corollary: if method in question private, resharper might analyze (though i'm not sure does) public code , verify there indeed no way particular private method called null reference, since public method, , 1 available through interface,

resharper wrong.


Comments

Popular posts from this blog

Linux vanilla kernel on QEMU and networking with eth0 -

rdbms - what exactly the undo information lives in oracle? -

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -