resharper - Should I use both NotNull and ContractAnnotation("null => halt")? -


it seems notnull , contractannotation("key: null => halt") similar in effect on r#. there missing? should apply both?

they're similar, semantically different.

notnull states target expected not null, makes no statements consequences. there might null check throws argumentnullexception, or might use without checking, , runtime nullreferenceexception. equally, application might check null, log , continue safely.

contractannotation("key: null => halt") tells resharper if key parameter null, program flow terminates. resharper can use so:

string foo = null; assert.notnull(foo);  // contractannotation("null => halt") assert.equal(12, foo.length); 

the second assert in snippet marked dead code, because resharper knows first assert throw when passed null.

however, if assert.notnull marked notnull attribute, resharper highlight foo parameter warning, telling you shouldn't passing null value, doesn't know happen if do.

subtle, different. i'd stick notnull attribute if require value never null, , use contractannotation("null => halt") assert style method , explicitly throw if passed null.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -