c# - Unexpected behavior using Enumerable.Empty<string>() -
i expect enumerable.empty<string>()
return empty array of strings. instead, appears return array single null
value. breaks other linq operators defaultifempty
, since enumerable not, in fact, empty. doesn't seem documented anywhere, i'm wondering if i'm missing (99% probability).
gameobject class
public gameobject(string id,ienumerable<string> keywords) { if (string.isnullorwhitespace(id)) { throw new argumentexception("invalid", "id"); } if (keywords==null) { throw new argumentexception("invalid", "keywords"); } if (keywords.defaultifempty() == null) { //this line doesn't work correctly. throw new argumentexception("invalid", "keywords"); } if (keywords.any(kw => string.isnullorwhitespace(kw))) { throw new argumentexception("invalid", "keywords"); } _id = id; _keywords = new hashset<string>(keywords); }
test
[testmethod] [expectedexception(typeof(argumentexception))] public void emptykeywords() { gameobject test = new gameobject("test",system.linq.enumerable.empty<string>()); }
it looks expect condition:
keywords.defaultifempty() == null
to evaluate true
. defaultifempty
returns singleton sequence containing default element type (string
in case) if source sequence empty. therefore return sequence containing null
. not null
condition returns false
.
Comments
Post a Comment