using System;
namespace LinqDemo
{
class Program
{
static void Main(string[] args)
{
//传统用法示例
Employee Emp = new Employee("Jimmy.Yang", 25);
Console.WriteLine(Emp.ToString());
Console.WriteLine("-------------------");
//自动属性的写法
NewEmployee NewEmp = new NewEmployee { Name = "Tom", Age = 30 };//感觉与Javascript中对象的JSON字符串表示法相似
Console.WriteLine(NewEmp.ToString());
Console.ReadLine();
}
}
/// <summary>
/// 传统方式定义一个类
/// </summary>
public class Employee
{
private string _name = "Anonymous";
private int _age = 0;
public string Name
{
get { return this._name; }
set { this._name = value; }
}
public int Age
{
get { return this._age; }
set { this._age = value; }
}
public Employee() { }
public Employee(string pName, int pAge)
{
this._name = pName;
this._age = pAge;
}
public override string ToString()
{
return "Name:" + this._name + " Age:" + this._age;
}
}
/// <summary>
/// .Net3.0自动属性的新写法
/// </summary>
public class NewEmployee
{
public string Name{get; set;}
public int Age { get; set; }
public override string ToString()
{
return "Name:" + this.Name + " Age:" + this.Age;
}
}
}
可以看出,.Net3.0的自动属性,可以使定义一个类的代码大大减化,个人感觉:这一点好象又是从Javascript中的JSON字符串表示法“偷”来的^_^,不信的话,可以参看以下Javascript代码:namespace LinqDemo
{
class Program
{
static void Main(string[] args)
{
//传统用法示例
Employee Emp = new Employee("Jimmy.Yang", 25);
Console.WriteLine(Emp.ToString());
Console.WriteLine("-------------------");
//自动属性的写法
NewEmployee NewEmp = new NewEmployee { Name = "Tom", Age = 30 };//感觉与Javascript中对象的JSON字符串表示法相似
Console.WriteLine(NewEmp.ToString());
Console.ReadLine();
}
}
/// <summary>
/// 传统方式定义一个类
/// </summary>
public class Employee
{
private string _name = "Anonymous";
private int _age = 0;
public string Name
{
get { return this._name; }
set { this._name = value; }
}
public int Age
{
get { return this._age; }
set { this._age = value; }
}
public Employee() { }
public Employee(string pName, int pAge)
{
this._name = pName;
this._age = pAge;
}
public override string ToString()
{
return "Name:" + this._name + " Age:" + this._age;
}
}
/// <summary>
/// .Net3.0自动属性的新写法
/// </summary>
public class NewEmployee
{
public string Name{get; set;}
public int Age { get; set; }
public override string ToString()
{
return "Name:" + this.Name + " Age:" + this.Age;
}
}
}
<script type="text/javascript">
var Emp = {Name:"Jimmy.Yang",Age:30};
function ShowEmp(E)
{
return "Name:" + E.Name + " Age:" + E.Age;
}
document.write(ShowEmp(Emp));
</script>
var Emp = {Name:"Jimmy.Yang",Age:30};
function ShowEmp(E)
{
return "Name:" + E.Name + " Age:" + E.Age;
}
document.write(ShowEmp(Emp));
</script>
顺便发表一下个人感慨:微软确实很善于吸引他人长处