33

How do I perform a null-check on a dynamic object?

Pseudo code:

public void Main() {
    dynamic dynamicObject = 33;
    if(true) { // Arbitrary logic
        dynamicObject = null;
    }
    Method(dynamicObject);
}

public void Method(dynamic param) {
    // TODO: check if the content of 'param' is equal to null
}
Seb Nilsson
  • 26,200
  • 30
  • 103
  • 130

4 Answers4

44

Are you worried about the possibility the dynamic object will have a custom equality operator that will change the way the null is interpreted? If so just use Object.ReferenceEquals

if (Object.ReferenceEquals(null, param)) {
  .......
}
Seb Nilsson
  • 26,200
  • 30
  • 103
  • 130
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • @Ani not sure what' your getting at there. – JaredPar Aug 11 '11 at 16:51
  • 3
    You could just write `ReferenceEquals(null, param)`. I can't believe from the ~5 questions on this topic at SO didn't solve it this simply. – Seb Nilsson Aug 11 '11 at 16:53
  • @JaredPar: Since all C# types that can contain method definitions extend `System.Object` (AFAIK), it isn't necessary to qualify. (Not all that important really.) – Ani Aug 11 '11 at 16:53
  • @Ani gotcha. I prefer the long hand version because it reduces the possibility that you bind to a more local `ReferenceEquals` implementation that has different behavior – JaredPar Aug 11 '11 at 16:56
  • 1
    @JaredPar: Fair enough. Of course, the containing type could contain a member called `Object` with an instance method called `ReferenceEquals` with different behaviour too.. – Ani Aug 11 '11 at 17:02
  • 2
    @Ani definitely. For me that borders on being evil and virtually every code sample can be defeated by someone being evil. – JaredPar Aug 11 '11 at 17:04
  • I struggeld a lot with this issue and tried a lot of options. This is the one I got the best result for what I wanted. Clean and clear. Thanks!! – Rodrigo.A92 Mar 26 '20 at 13:08
  • What about `is null` and `is not null` keywords? doesn't it do the same? – LordDraagon Jul 22 '22 at 05:20
0

You can use simplicity:

var s = data.servicePhoneNumber is null ? "" : data.servicePhoneNumber.Value;
Juan
  • 4,910
  • 3
  • 37
  • 46
0

Fast way might be:

if (_owner is null)
{

}
tedebus
  • 978
  • 13
  • 20
0

You can always just make the param of type object, that's what the compiler is doing. When you type a parameter dynamic it just means within that method only it is using dynamic invoke for all uses of param, but outside it's just a signature of type object. A more powerful usage of your dynamicObject would be to have overloads of the method you are calling, so if you keep your example the same and just have two overloads it would call one of the two methods based on the runtime type, and you can always add more for more types.

public void Main() {
    dynamic dynamicObject = 33;
    if(true) { // Arbitrary logic
        dynamicObject = null;
    }
    Method(dynamicObject);
}
public void Method(int param) {
  //don't have to check check null
  //only called if dynamicObject is an int
}
public void Method(object param) {
// will be called if dynamicObject is not an int or null
}
jbtule
  • 31,383
  • 12
  • 95
  • 128