C#非元音词

use*_*675 0 c# extension-methods

我想要一个需要返回非元音词的扩展方法.我设计了

 public static IEnumerable<T> NonVowelWords<T>(this IEnumerable<T> word)
    {
        return word.Any(w => w.Contains("aeiou"));
    }
Run Code Online (Sandbox Code Playgroud)

我收到错误,因为"T"不包含extanesion方法"Contains".

Luk*_*keH 14

如果你总是处理字符串,则不需要使用泛型方法.

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

    return words.Where(w => w.IndexOfAny(vowels) == -1);
}
Run Code Online (Sandbox Code Playgroud)