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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值