6

Is that possible in C# to know from MSDN docs that some method/property/field can or can NOT return null value?

e.g. Image.RawFormat Property from MSDN says:

The ImageFormat that represents the file format of this Image.

Can it return null? Should I perform such null check in my code or it ALWAYS be not null?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Michael Z
  • 3,883
  • 10
  • 43
  • 57

4 Answers4

4

I believe that for any input value (i.e. not provided by your code) you should perform validation checks. Even if you see in MSDN that it can't return null now, it may change in the future and it's a good practice anyway.

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
  • But if I will do like this I will get lower Code Coverage! Don't? – Michael Z Dec 27 '11 at 10:23
  • I miss understanding the use of "lower code coverage" at this thread. this phrase, as far as I know, is referring to the level of testing you done to your code. like "how many possible scenarios have you cover?". anyway it will make your code safer. the only down side is you loss performance but it's probably so low it won't be measurable. – Roee Gavirel Dec 27 '11 at 10:32
  • Yes, you understand me correctly. When I run some code coverage tool it measures how good is my code tested(with some xUnit framework) - so I will loose some coverage percents if I will have dead code that will never be invoked and thus not available for testing. – Michael Z Dec 27 '11 at 10:54
  • i'm not sure what this specific test does and measure. But what is more important to you? the score of this test or the robustness of your code? – Roee Gavirel Dec 27 '11 at 11:27
  • Both! And checking for null is not very good design this make code dirty? Don't you think? – Michael Z Dec 27 '11 at 13:24
  • It come this, how much can you count on given parameters. APIs, as rarely as it may, can change and I don't think Microsoft will mail you about it. and even if you would know about a change, do you want to re check all your code every time it happens. I put may trust in god, all the rest I check (-` – Roee Gavirel Dec 27 '11 at 13:50
1

I don't think there is a guaranteed way to tell from the documentation, but it would usually say if it can be null. For example, if you look at the documentation for System.Windows.Documents.Inline.NextInline, it says:

An Inline object representing the next Inline element that is a peer to this element, or null if there is no next Inline element.

Regardless, if your program cannot handle a null value and you have an instance of a reference type, you should still do the appropriate validation for null.

bobbymcr
  • 23,769
  • 3
  • 56
  • 67
  • Sadly, what you are saying about MSDN doesn't always apply. Look for example at http://msdn.microsoft.com/en-us/library/system.windows.application.current(v=vs.95).aspx . Of course it makes sense that the property is null in non-WPF application, but it is not explicitly stated there. – Matěj Zábský Dec 27 '11 at 09:30
  • @MatějZábský: Yes, documentation is not guaranteed to be explicit about this. It seems that if it is "important" (or common) enough to mention the possibility of null, it will be there. – bobbymcr Dec 27 '11 at 09:33
1

Resharper will warn you when you use object which cannot be proven not null (for example by looking at NotNull attribute, or by seeing that the called method does checks of its own) - and then unobtrusively offer you to add the check with a click or two.

This way you can avoid littering your code with checks where not necessary.

Also, often using Debug.Assert is just enough to do the "unlikely-but-what-if" checks (where you have to terminate whatever the code is doing anyways).

Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
0

There is no way to be sure that a nullable type will not return null, however consider the framework is stable in terms of code changes, I will tend to make an informed decision by looking at how the property or method you are calling works using ILSpy or JustDecompile.

In your example it would appear that null could not be returned as the constructor for ImageFormat accepts a Guid and assigns it to a private field.

Depending on what you are doing with the property it may be worth checking:

  1. That a StatusException is not thrown on accessing the property.
  2. That ImageFormat equals a format you are expecting as it could represent a image format that neither GdiPlus or .NET is "aware" of.
Richard Slater
  • 6,313
  • 4
  • 53
  • 81