It doesn't really matter whether you use if or when. The most important is to avoid buggy code within your catch. Check for "(obj is not null)" or "(obj is object)" and everything is fine. By the way: Don't use "object? ...". Simply write "object obj;" and as a member variable of a class it will be initialised to null, because it is a reference type. Nullable is only used for value types. (Or in printed documentation to make clear that null will be handled.)
@PatrickBorgogno
26 күн бұрын
I agree, but it is interesting to see that internally "catch when" is handled a little different. Nothing I would build on though. On the nullability part: I agree, that Nullable is only used for value types when you are talking about Nullable<T>. However, the nullable reference types feature in c# is something I activate on new code everywhere because it makes handling null values explicit. When turned on and you are dealing with "object" you know that it's never null. If you however allow it to be null you use "object?". Your IDE should highlight when you don't handle the null case on "object?". When code has this feature turned on, you don't have to dig into documentation or decompile source code to find out, if a function returns null in some cases. If it isn't turned on you never know without looking closely. And this is why I don't think that "object? ..." is the same as "object ..." when the nullable reference types feature is turned on as it was in my sample.
@mumk
Ай бұрын
glad you recovered from sickness
@Harambe899
Ай бұрын
Ich glaub ich bin verliebt!
@denoshanrajasingam6180
2 ай бұрын
Super erklärt mit verständlichen Grafiken. Freue mich auf das nächste Video.
@denoshanrajasingam6180
4 ай бұрын
Very exciting and interesting video. Very well explained with good reasoning. Looking forward to the next one.
Пікірлер