C# 从 1.0 到 4.0 代码写法上的改进

红薯 发布于 2011/04/14 08:03
阅读 798
收藏 6
C#

本文从一个雇员类(Employee)的定义来看看 C# 语言从 1.0 到 4.0 在代码写法风格上的变化。

C# 1.0

public class EmployeeCompare : IComparer
{
        public int Compare(object x, object y)
        {
            Employee emp1 = (Employee)x;
            Employee emp2 = (Employee)y;
            return emp1.Name.CompareTo(emp2.Name);
        }
}
public class Employee
    {
        string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        int _id;
        public int ID
        {
            get
            {
                return _id;
            }
        }
public Employee(int id, string name)
        {
            this._id = id;
            this._name = name;
        }

public ArrayList GetEmployee()
        {
            ArrayList al = new ArrayList();
            al.Add(new Employee(1, "pranay"));
            al.Add(new Employee(2, "kruanal"));
            al.Add(new Employee(2, "hanika"));
            return al;
        }

public void displayEmployee()
        {
            ArrayList alEmployee = GetEmployee();
            alEmployee.Sort();
            foreach (Employee emp in alEmployee)
            {
                Console.WriteLine(emp.Name);
            }
        }
    }

C# 2.0

public class EmployeeCompare : IComparer<Employee>
    {
        public int Compare(Employee x, Employee y)
        {
            return x.Name.CompareTo(y.Name);
        }
    }

public class Employee
    {
        string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        int _id;
        public int ID
        {
            get
            {
                return _id;
            }
            private set
            {
                _id = value;
            }
        }

public Employee(int id, string name)
        {
            this._id = id;
            this._name = name;
        }

public List<Employee> GetEmployee()
        {
            List<Employee><employee> al = new List<Employee>();
            al.Add(new Employee(1, "pranay"));
            al.Add(new Employee(2, "kruanal"));
            al.Add(new Employee(2, "hanika"));
            return al;
        }
</employee>

public void displayEmployee()
        {
            List<Employee> alEmployee = GetEmployee();
            //alEmployee.Sort();

            alEmployee.Sort(delegate(Employee x, Employee y)
            { return x.Name.CompareTo(y.Name); });

            foreach (Employee emp in alEmployee)
            {
                Console.WriteLine(emp.Name);
            }
        }
    }

C# 3.0

public class Employee
    {
        public string Name
        {
            get;
            set;
        }

        public int ID
        {
            get;
            private set;
        }

Employee() { }

        public Employee(int id, string name)
        {
            this.ID = id;
            this.Name = name;
        }

public List<Employee> GetEmployee()
        {
            return new List<Employee>
            {
                new Employee{ Name="pranay", ID=1},
                new Employee{ Name="krunal", ID=2},
                new Employee{ Name="hanika", ID=3}
            };
        }

public void displayEmployee()
        {
            List<Employee> alEmployee = GetEmployee();
            alEmployee.Sort((x,y) => x.Name.CompareTo(y.Name) );
            //alEmployee.OrderBy(e => e.Name);
            foreach (Employee emp in alEmployee)
            {
                Console.WriteLine(emp.Name);
            }
        }
    }

C# 4.0

public class Employee
    {
        public string Name
        {
            get;
            set;
        }

        public int ID
        {
            get;
            private set;
        }

public Employee(string name, int id = 0)
        {
            this.ID = id;
            this.Name = name;
        }

public List<Employee> GetEmployee()
        {
            return new List<Employee>           {
                new Employee(name : "pranay", id :1 ),
                new Employee(name : "krunal", id :2 ),
                new Employee(name : "hanika", id :3 )
            };
        }

        public void displayEmployee()
        {
            List<Employee> alEmployee = GetEmployee();
            //alEmployee.Sort((x,y) => x.Name.CompareTo(y.Name) );
            alEmployee.OrderBy(e => e.Name);
            foreach (Employee emp in alEmployee)
            {
                Console.WriteLine(emp.Name);
            }
        }
    }

语言本身的进化,代码越来越简洁。

加载中
0
Kaede
Kaede

微软技术还是比较人性化的

0
k
kingdizzy

引用来自#2楼“Kaede”的帖子

微软技术还是比较人性化的

是越来越简洁了,且人性化了,但是里面封装的越来越多,如果不理解底层都不知道干了啥

返回顶部
顶部