开发者

C# non-vowel words

I want an extension method that needs to return non-vowel words.I designed

 public static IEnumerable<T> NonVowelWords<T>(this IEnumerable<T> word)
    {
        return word.Any(w => w.开发者_开发问答Contains("aeiou"));
    }

I received error as "T" does not contain extanesion method "Contains".


You don't need to use a generic method if you're always dealing with strings.

public static IEnumerable<string> NonVowelWords(this IEnumerable<string> words)
{
    char[] vowels = { 'a', 'e', 'i', 'o', 'u' };

    return words.Where(w => w.IndexOfAny(vowels) == -1);
}


Try

public static IEnumerable<string> NonVowelWords<T>(this IEnumerable<string> word)
{
    return word.Where(w => !(w.Contains("a") || w.Contains("i") || w.Contains("u") || w.Contains("e") || w.Contains("o")));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜