0

I need to write validation method that validates if non-nullable/nullable object is valid.

For example:

public void Example()
{
    object obj = null; //Can't be null.
    object? obj2 = null; //Can be null.
    object obj3 = new(); //Can't be null.
    object? obj4 = new(); //Can be null.

    var objIsValid = ObjIsValid(obj); //false
    var obj2IsValid = ObjIsValid(obj2); //true
    var obj3IsValid = ObjIsValid(obj3); //true
    var obj4IsValid = ObjIsValid(obj4); //true
}

How to make method like this?

public bool ObjIsValid(object obj)
{
     if(!obj.MarkedWithQuestionMark(obj) && obj is null) 
          return false;
     return true;
}
Yoro
  • 805
  • 8
  • 17
  • This question can help you: https://stackoverflow.com/questions/374651/how-to-check-if-an-object-is-nullable – Tolga Cakir Jul 18 '22 at 13:33
  • No, it didn't help. There in answers they are checking if object is nullabe. And the thing is, that object is always nullable even if it's not marked with question mark. I want to check specifically if it is marked with question mark or not. – Yoro Jul 18 '22 at 13:44
  • 2
    A method like that can't be written, because the "marked with a question mark" thing you're talking about is a nullable reference annotation. That exists at compile time, and to some extent at runtime where attributes are emitted on declarations, but it does *not* exist as part of a *value*. By and large a method like that doesn't *need* to be written either, because the compiler will already emit a warning on the `object obj = null` line, which is the entire point of NRTs. – Jeroen Mostert Jul 18 '22 at 13:51

1 Answers1

2

You simply write it is such:

public bool ObjIsValid(object obj) => obj is not null;

In other words, I wouldn't consider whether the original type was nullable or not at all. Nullalbe Reference Types are a feature for static analysis. During runtime there is nothing preventing a variable that was not defined as a nullable reference to contain a null (also see this Q&A).

It is just not worth the effort to check if something could be null and only check it then. Simply check it, and be done with it.

Besides, even if you really wanted to write a MarkedWithQuestionMark(object obj) function, the point is you can't. There is no way to check if a variable was annotated as a nullable reference type. This information is only available during compile time. There is no meta information left in the IL that can help here.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
  • "During runtime there is nothing preventing a variable that was not defined as a nullable reference to contain a null" - I don't want to prevent it. I need that validation method, just to log, that there was null value, where it shouldn't be, for future investigation of code structure/architecture. – Yoro Jul 18 '22 at 13:55
  • 1
    The point is you can't. There is no way to check if a variable was annotated as a nullable reference type. This information is only available during compile time. Also, even there would be way, the information is useless, because you cannot rely on it (see links in my answer). – Christian.K Jul 18 '22 at 13:57
  • Can you add this your comment to the answer, so I can accept it as an answer? – Yoro Jul 18 '22 at 14:09