C#: How to perform a null-check on a dynamic object

30,761

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)) {
  .......
}
Share:
30,761
Seb Nilsson
Author by

Seb Nilsson

Software Developer with focus on ASP.NET & C# Passionate about the web, HTML5 & JavaScript Active within Microsoft-technologies and Open Source Sharing knowledge from own projects

Updated on April 02, 2020

Comments

  • Seb Nilsson
    Seb Nilsson over 4 years

    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
    }