-
Is it a bug that the compiler can't figure out that Bar does get initialized in the constructor? In the explicit interface implementation I get a CS8618 warning: Non-nullable IFoo.Bar must contain a non-null value when exiting constructor How is nullable reference types supposed to be used with explicit interface implementations? public interface IFoo
{
string Bar { get; set; }
}
public class FooExplicit : IFoo
{
public FooExplicit(string bar)
{
if (bar is null) throw new ArgumentNullException(nameof(bar));
((IFoo)this).Bar = bar;
}
string IFoo.Bar { get; set; }
}
public class FooImplicit : IFoo
{
public FooImplicit(string bar)
{
if (bar is null) throw new ArgumentNullException(nameof(bar));
Bar = bar;
}
public string Bar { get; set; }
} Thanks for any help |
Beta Was this translation helpful? Give feedback.
Answered by
ilbrando
Mar 2, 2022
Replies: 1 comment 11 replies
-
I found a work around. Using a private field. public interface IFoo
{
string Bar { get; set; }
}
public class FooExplicit : IFoo
{
private readonly string _bar;
public FooExplicit(string bar)
{
if (bar is null) throw new ArgumentNullException(nameof(bar));
_bar = bar;
}
string IFoo.Bar => _bar;
} |
Beta Was this translation helpful? Give feedback.
11 replies
Answer selected by
ilbrando
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found a work around. Using a private field.