2016-08-19 22 views
-3

//如何創建一個方法,使下面的代碼將工作如何創建一個方法,使下面的代碼將工作

const string abc = "asduqwezxc"; 
foreach (var vowel in abc.SelectOnlyVowels()) 
{ 
    Console.WriteLine("{0}", vowel); 
} 
+0

在const的代碼開始 – BTJ

+1

突出的代碼,然後點擊右鍵並選擇狀態選項'Refactor->提取Method' – MethodMan

+0

我敢肯定,他問該怎麼寫'SelectOnlyVowels'串擴展 – Jonesopolis

回答

4

你需要寫an extension method,就像這樣:

public static class StringExt 
{ 
    public static IEnumerable<char> SelectOnlyVowels(this string self) 
    { 
     return self.Where(c => "aeiou".Contains(char.ToLower(c))); 
    } 
} 
相關問題