1: using System;
2: using System.Collections.Generic;
3:
4: namespace LambdaExpressionDemo
5: {
6: class Program
7: {
8: private static List<StudentData> _class = null;
9:
10: static void Initialization()
11: {
12: _class = new List<StudentData>();
13:
14: _class.Add(new StudentData("ZeroCool", 24, GenderType.Male, 87));
15: _class.Add(new StudentData("Michael", 24, GenderType.Male, 93));
16: _class.Add(new StudentData("Frieda", 22, GenderType.Female, 98));
17: _class.Add(new StudentData("Somebody", 23, GenderType.Male, 81));
18: }
19:
20: static void Main(string[] args)
21: {
22: Initialization();
23:
24: if (_class == null || _class.Count == 0)
25: {
26: throw new InvalidOperationException("The system initialization was failed.");
27: }
28:
29: StudentData studentData = _class.Find(student => student.MathScore >= 90 && student.MathScore < 95);
30: RepresentData(studentData);
31:
32: studentData = _class.Find(student => student.Name.Equals("ZeroCool", StringComparison.InvariantCultureIgnoreCase));
33: RepresentData(studentData);
34:
35: studentData = _class.Find(student => student.Age < 23);
36: RepresentData(studentData);
37:
38: Console.ReadLine();
39: }
40:
41: static void RepresentData(StudentData student)
42: {
43: if (student == null)
44: {
45: Console.WriteLine("No mached student.");
46:
47: return;
48: }
49:
50: Console.WriteLine("Name:\t\t" + student.Name);
51: Console.WriteLine("Age:\t\t" + student.Age);
52: Console.WriteLine("Gender:\t\t" + student.Gender.ToString());
53: Console.WriteLine("Math Score:\t" + student.MathScore);
54: Console.WriteLine();
55: }
56: }
57:
58: public enum GenderType
59: {
60: Male = 0,
61: Female
62: }
63:
64: public class StudentData
65: {
66: private string _name = String.Empty;
67: public string Name
68: {
69: get { return this._name; }
70: set { this._name = value; }
71: }
72:
73: private int _age = 0;
74: public int Age
75: {
76: get { return this._age; }
77: set { this._age = value; }
78: }
79:
80: private GenderType _gender;
81: public GenderType Gender
82: {
83: get { return this._gender; }
84: set { this._gender = value; }
85: }
86:
87: private int _mathScore = 0;
88: public int MathScore
89: {
90: get { return this._mathScore; }
91: set { this._mathScore = value; }
92: }
93:
94: public StudentData()
95: {
96: }
97:
98: public StudentData(string name, int age, GenderType gender, int mathScore)
99: {
100: this._name = name;
101: this._age = age;
102: this._gender = gender;
103: this._mathScore = mathScore;
104: }
105:
106: public delegate void EmptyDelegate();
107:
108: EmptyDelegate dl = () => Console.WriteLine();
109: }
110: }