This Question is Not Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
2 Replies Last post: Feb 25, 2013 5:49 PM by Martyn Bone  
Martyn Bone Newbie 2 posts since
Feb 17, 2013
Currently Being Moderated

Feb 17, 2013 8:49 AM

Howto indicate all items in a collection are NotNull with ContractAnnotation

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;
        }
    }
}
Alex Berezoutsky JetBrains 456 posts since
Feb 18, 2011
Currently Being Moderated
Feb 20, 2013 4:09 PM in response to: Martyn Bone
Re: Howto indicate all items in a collection are NotNull with ContractAnnotation

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.

Attachments:

More Like This

  • Retrieving data ...