20120828

集合:

  基本的集合功能:IEnumerable 可迭代集合中的项目;ICollection(继承于IEnumerable)获取集合个数,并把项复制到简单数组类型;IList(继承于IEnumerable,ICollection)提供集合的项列表,并可以访问这些项目,以及其他功能;IDictionary,类似IList,且可以通过键码值获取;

   ArrayList的AddRang()添加多个数据,支持ICollection接口。

   CollectionBase类有接口IEnumerable、ICollection 和IList。并提供两个保护属性List和InnerList,List通过IList访问项,InnerList用于储存项的ArrayList对象。

   可以通过foreach进行遍历返回对象。

    DictionaryBase类类似于CollectionBase,区别DictionaryBase返回DictionaryEntry,需要DictionaryEntry.Value返回对象

迭代器:

   Foreach运行过程,在迭代集合collectionobject集合中:

   1)调用collectionobject.GetEnumerator()

   2)MoveNext()?使用IEnumerator.Current = 当前引用:结束;

   迭代器中使用Yield关键词选择Foreach循环中使用的值。

public static IEnumerable SimpleList ()
{
    yield return "string 1";
    yield return "string 1";
    yield return "string 1";
}

public static void Main( string []  args )
{
    foreach ( string item in SimpleList() )
    {
         Console.WriteLine(item);
    }
     Console.ReadKey();
}

运行过程:Foreach每遍历一次(请求一次结果)才运行一次yield,上图中SimpleList会中断。

IS运算符

检查对象是否是给定的类型,或者可以转化为给定的类型。 <operand> is <type>

可以重载的运算符:

  一元:+,-,!,~,++,--,true,false

  二元:+,-,*,/,%,&,|,^,<<,>>

  三元: ==,!=,<,>,<=,>=

IComparable和IComparer接口

区别: IComparable在要比较的类中实现,比较该类与另一个对象

IComparer在单独的一个类中实现,比较任意两个对象。

类Comparer提供了IComparer接口的默认实现方式

(Comparer.Default.Compare( obj, obj))

 

As运算符,将一种类型转换为另一种引用类型。

<operand> as <type>,但是只能运用于: <operand>类型是 /可以隐私转换为/可以封箱到<type>类型3种情况。否则返回NULL

泛型

可空类型:Nullable

例子:

System.Nullable<int> nullInt ;
nullInt = null 

等价于:

nullInt = new System.Nullable<int>();

常使用以下简化:

int ? nullInt;

 

 

??运算符

 

op1 ?? op2

等价于:

op1 == op2 ? op1 : op2;

其中op1为任意可空表达式(引用,可空类型)

 

定义泛型(泛型的使用)

针对于泛型算法中泛型变量的初始化,值类型不能为null,所以不确定。一个泛型类型传入类型为值类型或者引用类型,因此也不能给定其一个正确的默认值(值类型为0,引用为null)

针对于泛型中只能使用特定集中类型,可以使用关键词where

class MyClass<T> where T : int, double ,classA
{
    ... ...
}
class MyClass<T1, T2> where T1 : int where T2 :classA
{
    ... ...
}
posted @ 2012-08-31 09:53  Caius.Walt.Wang  阅读(161)  评论(0编辑  收藏  举报