我寫了下面的代碼,找出失蹤的序列中定義,但正如我所說,這個我得到的錯誤是我的代碼擴展方法必須在非泛型靜態類
public partial class Missing : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<int> daysOfMonth =
new List<int>() { 6, 2, 4, 1, 9, 7, 3, 10, 15, 19, 11, 18, 13, 22, 24, 20, 27, 31, 25, 28 };
Response.Write("List of days:");
foreach (var num in daysOfMonth)
{
Response.Write(num);
}
Response.Write("\n\nMissing days are: ");
// Calling the Extension Method in the List of type int
foreach (var number in daysOfMonth.FindMissing()){Response.Write(number);}
}
public static IEnumerable<int> FindMissing(this List<int> list)
{
// Sorting the list
list.Sort();
// First number of the list
var firstNumber = list.First();
// Last number of the list
var lastNumber = list.Last();
// Range that contains all numbers in the interval
// [ firstNumber, lastNumber ]
var range = Enumerable.Range(firstNumber, lastNumber - firstNumber);
// Getting the set difference
var missingNumbers = range.Except(list);
return missingNumbers;
}
}
我得到的錯誤如下Extension method must be defined in a non-generic static class
任何一個可以幫助我
的可能的複製[擴展方法必須在非泛型靜態類中定義(https://stackoverflow.com/questions/6096299/extension -methods-must-defined-in-a-non-generic-static-class) – amin 2017-08-21 05:35:20