C#中的關鍵字Partial,where,yieId!

//Partial:
//     這是C#2.0的新特性 Partial關鍵字起到把一個class分段組合作用,能用於多個接口或一個繼承
//代碼片斷:

namespace Com.TommyLib
{
    
public interface A
    
{
        
void A_Fun1();
        
void A_Fun2();
    }

}
 

//新建B.cs

namespace Com.TommyLib
{
    
public partial class B
    
{
        
public B()
        

        }

    
    }



    
public partial class B : A //這裏用Partial 分段加入B.cs 有 interface A接口
    {
         
public void A_Fun1()
        
{
            
throw new NotImplementedException();
        }


        
public void A_Fun2()
        
{
            
throw new NotImplementedException();
        }


   }

}


//新建C.cs

namespace Com.TommyLib
{
    
public class C
    
{
        
public C()
        
{
          
        }


        
protected void C_Fun()
        
{
            Console.WriteLine(
"C_Fun");
        }


        
public void C_Fun1()
        
{
            Console.WriteLine(
"C_Fun2");
        }

    }


    
public class D
    
{

    }


    
public interface E
    
{
        
void E_Fun();
    }



    
public partial class B:C //partial 分段 class B 這裏繼承了 class C
    {
        
public void B_Fun()
        
{
            Console.WriteLine(
"B_FUN");
            C_Fun();
        }

    }


/*

//不能繼承多個類,B繼承了class C,不能繼承classD ,遵循C#不能多重繼函的規定

   public partial class B : D
    {

    }

*/



    
public partial class B : E //可以是多重接口,這裏class B 有 interface E接口
    {


        
E Members
    }


}



//A.cs.B.cs C.cs 三個文件有二個interface.三個class ,class B 有 A,E兩個接口.繼承於class C.

//C#2.0 partial關鍵字能夠一個class 文件分成幾部分寫.遵循C#不可多重繼承但可以有多個接口的法則.
where(C# 參考)

 

where 子句用於指定類型約束,這些約束可以作爲泛型聲明中定義的類型參數的變量。例如,可以聲明一個泛型類 MyGenericClass,這樣,類型參數 T 就可以實現 IComparable<T> 接口:

public class MyGenericClass<T> where T:IComparable { }

除了接口約束,where 子句還可以包括基類約束,以指出某個類型必須將指定的類作爲基類(或者就是該類本身),才能用作該泛型類型的類型參數。這樣的約束一經使用,就必須出現在該類型參數的所有其他約束之前。

// cs_where.cs
// compile with: /target:library
using System;

class MyClassy<T, U>
    
where T : class
    
where U : struct
{
}

where 子句還可以包括構造函數約束。可以使用 new 運算符創建類型參數的實例;但類型參數爲此必須受構造函數約束 new() 的約束。new() 約束可以讓編譯器知道:提供的任何類型參數都必須具有可訪問的無參數(或默認)構造函數。例如:

// cs_where_3.cs
// compile with: /target:library
using System;
using System.Collections;

interface MyI
{
}


class Dictionary<TKey,TVal>
    
where TKey: IComparable, IEnumerable
    
where TVal: MyI
{
    
public void Add(TKey key, TVal val)
    
{
    }

}

還可以將約束附加到泛型方法的類型參數,例如:

public bool MyMethod<T>(T t) where T : IMyInterface { }
請注意,對於委託和方法兩者來說,描述類型參數約束的語法是一樣的:
delegate T MyDelegate<T>() where T : new()
yield(C# 參考)

 

迭代器塊中用於向枚舉數對象提供值或發出迭代結束信號。它的形式爲下列之一:

 

yield return <expression>;
yield break;

計算表達式並以枚舉數對象值的形式返回;expression 必須可以隱式轉換爲迭代器的 yield 類型。

yield 語句只能出現在 iterator 塊中,該塊可用作方法、運算符或訪問器的體。這類方法、運算符或訪問器的體受以下約束的控制:

  • 不允許不安全塊。

  • 方法、運算符或訪問器的參數不能是 refout

yield 語句不能出現在匿名方法中。有關更多信息,請參見匿名方法(C# 編程指南)

當和 expression 一起使用時,yield return 語句不能出現在 catch 塊中或含有一個或多個 catch 子句的 try 塊中。有關更多信息,請參見異常處理語句(C# 參考)

在下面的示例中,迭代器塊(這裏是方法 Power(int number, int power))中使用了 yield 語句。當調用 Power 方法時,它返回一個包含數字冪的可枚舉對象。注意 Power 方法的返回類型是 IEnumerable(一種迭代器接口類型)。

// yield-example.cs
using System;
using System.Collections;
public class List
{
    
public static IEnumerable Power(int number, int exponent)
    
{
        
int counter = 0;
        
int result = 1;
        
while (counter++ < exponent)
        
{
            result 
= result * number;
            
yield return result;
        }

    }


    
static void Main()
    
{
        
// Display powers of 2 up to the exponent 8:
        foreach (int i in Power(28))
        
{
            Console.Write(
"{0} ", i);
        }

    }

}

輸出

2 4 8 16 32 64 128 256 
轉自MSDN地址:http://msdn.microsoft.com/zh-cn/library/9k7k7cf0(VS.80).aspx
發佈了33 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章