Howto indicate all items in a collection are NotNull with ContractAnnotation Follow
If i have an simple class as below containing a collection where i know that the collection can never have null added to it, is there any way to tell resharper this so that i would not get a warning on the indicated line (item may be null).
At the moment i tend to put a Debug.Assert(item!=null) wich prevents the warning on debug build with no release overhead. Other than that i could wrap all the standard collection types with wrapper classes or use an IEnumerator<T> extension (e.g. foreach (var item in items.WithNoNulls()) but this all seems a bit messy just to remove the resharper warnings.
public class MyClass { [NotNull] private readonly List<IMapItem> items = new List<IMapItem>(); public void Add([NotNull] IMapItem item) { items.Add(item); } private void DoWord() { foreach (var item in items) { string s = item.Name; } } }
Please sign in to leave a comment.
Hi Martyn,
Actually, if JetBrains.Annotations are referenced to this code (to use ReSharper's [NotNull] attribute), I do not get any warnings on the code you provided.
I'm sorry if I didn't understand you correct. If so, could you please provide some additional details on this situation.
Thanks.
Attachment(s):
screen431.png
Forget to mention that this is with the following resharper option set:
Assume Entity value can be null = When entity does not have an explicit NotNull Attribute.
-
Essentially everything that does not have a [NotNull] attribute is assumed to be [CanBeNull]. I have found this to benificial if it is used from the start of a project as it requires all code to be explicit about exactly where null are allowed or not (requiring the issue to be though about when writting the code) as well as the the actual warning being more through.
Looking for the same thing. Has anyone know if it's possible ?