C#3.0新增功能快速預覽

來源:互聯網
上載者:User

1.通過var關鍵字實現靈活的型別宣告:
class ImplicitlyTypedLocals2
{
    static void Main()
    {           
        string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" };

        // If a query produces a sequence of anonymous types,
        // then you must also use var in the foreach statement.
        var upperLowerWords =
             from w in words
             select new { Upper = w.ToUpper(), Lower = w.ToLower() };

        // Execute the query
        foreach (var ul in upperLowerWords)
        {
            Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower);
        }
    }       
}
2.更加靈活的物件建構方式:
private class Cat
{
    // Auto-implemented properties
    public int Age { get; set; }
    public string Name { get; set; }
}
static void MethodA()
{
    // Object initializer
    Cat cat = new Cat { Age = 10, Name = "Sylvester" };
}
3.集合裝配的特殊方法:
IEnumerable<int> highScoresQuery =
    from score in scores
    where score > 80
    orderby score descending
    select score;
4.對已存在的類型定義進行方法擴充:
namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }  
}
using ExtensionMethods;
string s = "Hello Extension Methods";
int i = s.WordCount();
5.隱式集合類型定義:
var v = new { Amount = 108, Message = "Hello" };
var productQuery =
    from prod in products
    select new { prod.Color, prod.Price };

foreach (var v in productQuery)
{
    Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price);
}

6.Lambda文法:
delegate int del(int i);
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25
7.查詢文法:
class LowNums
{
    static void Main()
    {  
        // A simple data source.
        int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

        // Create the query.
        // lowNums is an IEnumerable<int>
        var lowNums = from num in numbers
            where num < 5
            select num;

        // Execute the query.
        foreach (int i in lowNums)
        {
            Console.Write(i + " ");
        }
    }       
}
// Output: 4 1 3 2 0
8.智能簡約的訪問器聲明:
class LightweightCustomer
{
    public double TotalPurchases { get; set; }
    public string Name { get; private set; } // read-only
    public int CustomerID { get; private set; } // read-only
}

LightweightCustomer obj = new LightweightCustomer();
obj.TotalPurchases = 1;
Console.WriteLine(obj.TotalPurchases.ToString());

9.支援借口,類,結構,方法,特性的partial聲明:
partial class Earth : Planet, IRotate { }
partial class Earth : IRevolve { }
 [System.SerializableAttribute]
partial class Moon { }
[System.ObsoleteAttribute]
partial class Moon { }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.