C#3.0新特性 和 Javascript

來源:互聯網
上載者:User

這段是時間學習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());

這個例子可以說沒有任何義意,只是說明它們很像而已。

它們應該不只這兩個地方很像,在以後的學習和使用中,再去慢慢發現它吧

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.