泛型
減少"型別轉換的時間"大大增加程式效能
種類1:
public class Employee
{
public string Name { get; set; }
public int ID { get; set; }
public double Salary { get; set; }
}
List<Employee> Employees = new List<Employee>();
Employees.Add(new Employee { Name = "Willy", ID = 0001, Salary = 35000 });
Employees.Add(new Employee { Name = "Yummi", ID = 0002, Salary = 30000 });
Employees.Add(new Employee { Name = "Miri", ID = 0003, Salary = 20000 });
// 沿用 2.0 的 delegate 的方式
//Employees.Sort((emp1, emp2) => emp1.Salary.CompareTo(emp2.Salary));
// 3.0 新的方式
//由小到大
IEnumerable orderedEmp1 = Employees.OrderBy(emp => emp.Salary);
//由大到小
IEnumerable orderedEmp2 = Employees.OrderByDescending(emp => emp.Salary);
foreach (var p in orderedEmp2)
{
string s = ((Employee)p).Name;
}
//收尋薪水大於 20K 的員工
IEnumerable EmpsWithBigSalary = Employees.Where((emp) => emp.Salary > 20000);
foreach (var p in EmpsWithBigSalary)
{
string s = ((Employee)p).Name;
}
//使用 Linq
IEnumerable EmpsWithBigSalarybyLinq = from Employee emp in Employees
where emp.Salary > 20000
select emp;
//取的唯一的值
IEnumerable DistinctEmps = Employees.Distinct();
//將 Name 轉成 List
IEnumerable empNames = Employees.Select((emp) => emp.Name);
//將 ID 轉成 List
IEnumerable empIDs = Employees.Select((emp) => emp.ID);
//==================================================================================================
種類2 List
List<int> list = new List<int>();
list.Add(3);
//for(int i=0;i<list.Count;i++)
int count = list.Count;
if (list.Contains(3))
MessageBox.Show("yes get it");
//==================================================================================================
種類3
/// <summary>
/// 在定義泛型類別時不指定某些變數的具體類型,而是用一個類型參數代替,在使用這個類別時,這個類型的參數會由,一個具體的類型所代替,
/// </summary>
/// <typeparam name="T"></typeparam>
public class Record<T>
{
private string _name;
private T _grade;
public Record(string name, T grade)
{
this._name = name;
this._grade = grade;
}
public T Getgrade()
{
return this._grade;
}
}
//==================================================================================================
種類4
public class Record2<T>
{
private string _name;
private T _grade;
public Record2(string name, T grade)
{
this._name = name;
this._grade = grade;
}
public T Getgrade()
{
return this._grade;
}
}
//==================================================================================================
種類5
/// <summary>
/// 泛型類別可以支援一個以上的泛型變數,兩者型態一致。
/// </summary>
/// <typeparam name="T"></typeparam>
public class Record3<T>
{
private string _name;
private T _math;
private T _phy;
public Record3(string name,T math,T phy)
{
this._name = name;
this._math = math;
this._phy = phy;
}
public T GetMath()
{
return this._math;
}
public T GetPhy()
{
return this._phy;
}
}
//==================================================================================================
種類6
/// <summary>
/// 兩者型態也可不一致,就要宣告兩個型態的關鍵字。
/// </summary>
/// <typeparam name="S"></typeparam>
/// <typeparam name="T"></typeparam>
public class Record4<S, T>
{
private string _name;
private T _math;
private S _phy;
public Record4(String name, S phy, T math)
{
this._name = name;
this._math = math;
this._phy = phy;
}
public T GetMath()
{
return this._math;
}
public S GetPhy()
{
return this._phy;
}
}