Easy creation of a wrapper class that implements an interface #4881
Answered
by
333fred
michaelcheers
asked this question in
Language Ideas
-
Example: var list = new SuperList(new());
list.Add("Hello");
list.Add("Hello World"); // Cannot add "Hello World"
public class AlphabeticList : IList<string>
{
public List<string> _innerList;
public IList<string> => _innerList;
public AlphabeticList (List<string> innerList) => _innerList = innerList;
public new void Add (string value)
{
if (value.Any(c => !char.IsLetter(c))) throw new ArgumentException($"Cannot add {value}.");
base.Add(value);
}
} Right now, the lightbulb will recommend "Implement ... through ...". I propose we make this a language feature. |
Beta Was this translation helpful? Give feedback.
Answered by
333fred
Jun 26, 2021
Replies: 2 comments 4 replies
-
Why would you not just inherit public class AlphabeticList : List<string>
{
public AlphabeticList(IEnumerable<string> strings) => AddRange(strings);
public new void Add(string value)
{
if (value.Any(c => !char.IsLetter(c))) throw new ArgumentException($"Cannot add {value}.");
base.Add(value);
}
} |
Beta Was this translation helpful? Give feedback.
4 replies
-
Duplicate of #234. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
svick
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Duplicate of #234.