Is there a .Equals function in C# that takes into account nulls? -


i have code:

var upd = newobj.where(n => oldobj.any(o =>   (o.subtopicid == n.subtopicid) &&    (o.number != n.number ||    !o.name.equals(n.name) ||     !o.notes.equals(n.notes)))) .tolist(); 

what noticed when notes null .equals fails.

.","exceptionmessage":"object reference not set instance of object.","exceptiontype":"system.nullreferenceexception","stacktrace":" @ webrole.controllers.topiccontroller.<>c__displayclass8_1.b__1(subtopic o) in c:\h\server\webrole\controllers\web api - data\topiccontroller.cs:line 118\r\n @ system

is there way .equals take account either o.notes or n.notes null? ideally wondering if have function kind of check in quite few places in code.

you use static object.equals(object obja, object objb) method.

given derives object, can omit object , call equals, e.g:

 oldobj.any(o => !equals(o.name, n.name) ... 

this handles case either of arguments null returning false , returns true both arguments null.

per the docs:

  • it determines whether 2 objects represent same object reference. if do, method returns true. test equivalent calling referenceequals method. in addition, if both obja , objb null, method returns true.
  • it determines whether either obja or objb null. if so, returns false.
  • if 2 objects not represent same object reference , neither null, calls obja.equals(objb) , returns result. means if obja overrides object.equals(object) method, override called.

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -