這段是時間學習LINQ的同時學習了C#3.0的一些新特性,發學它和JavaScript很像
一、C#新特性的 自動屬性 & JavaScript JSON
C#代碼:
public class Person
{
public Person() { }
public string Name { get; set; }
public int Age { get; set; }
}
//建立一個Person對象
Person person = new Person { Name="Southsea", Age=22 };
JS代碼:
var person = {name:"Southsea",age:22};
//下面可以通過person來調用它的name屬性和age屬性
alert(person.name+" "+person.age);
相似這處:在建立一個對象的時候可以直接給屬性賦值
C#是用 " = " 而 JS 是 " : "
二、C# 擴充方法 & JS 中的 prototype
C#代碼:
//定義擴充方法
public static String ToMyString(this DateTime dt)
{
return dt.Year + "-" + dt.Month + "-" + dt.Day;
}
//擴充方法的調用
DateTime today = DateTime.Now;
today.ToMyString();
JS代碼:
Date.prototype.toMyString = function(){
return this.getFullYear()+ "-" + (this.getMonth()+1) + "-" + this.getDate();
}
var today = new Date();
alert(today.toMyString());
這個例子可以說沒有任何義意,只是說明它們很像而已。
它們應該不只這兩個地方很像,在以後的學習和使用中,再去慢慢發現它吧