Let's say I have an interface with a read-only property and and a concrete class where the property is instantiated in the constructor and marked as read-only.
internal interface IExample
{
ObservableCollection<string> Items { get; }
}
internal class Example : IExample
{
private readonly ObservableCollection<string> _items;
public Example()
{
_items = new ObservableCollection<string>();
}
public ObservableCollection<string> Items
{
get { return _items; }
}
}
When I use the interface Resharper warns me that I might have a possible null reference in calling code.
public class ExampleWithWarnings
{
public void Show()
{
IExample example = new Example();
// resharper warns about null reference
example.Items.Add( "test" );
}
}
I realize that by definition the interface doesn't guarantee that the property will have a value. (I also recognize that properties on interfaces aren't ideal). But I know this property will always have a value.
Is there any magic attribute that I can put on the interface that would prevent Resharper from showing a warning? I'd rather not have to decorate all usages of the class with a disable pragma warning.