程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> C#3.0新增功能快速預覽

C#3.0新增功能快速預覽

編輯:.NET實例教程

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)
        {
       &nbs

$False$

p;    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 { }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved