C# 札记

来源:互联网 发布:网络舆情汇报材料 编辑:程序博客网 时间:2024/06/16 00:35

1)public new [void FunctionName]/[StructName] 中的new

      这是C#特有的,因为父类有一个函数的函数名和参数列表和你这个类的这个函数一样,这个函数会隐藏父类里的那个同名同参数列表的函数。如果不用new,编译器会给个警告,如果用了编译器认为你知道你自己正在隐藏父类的同名函数,这时就正常了,C#建议在隐藏父类函数时都用这个关键字。例如:

 public class ClassBase
{
  public struct Default
  {
    public static bool Flag = true;
  }
}
public class Demo : ClassBase
{
  public new struct Default
  {
    public static bool Flag = false;
  }
}

 

2)C# yieldreturn

yield(C# 参考)

在迭代器块中用于向枚举数对象提供值或发出迭代结束信号。
它的形式为下
列之一:
yield return <expression>;
yield break;
备注:
计算表达式并以枚举数对象值的形式返回;expression 必须可以隐式转换为迭代器的 yield 类型。

yield 语句只能出现在 iterator 块中,该块可用作方法、运算符或访问器的体。这类方法、运算符或访问器的体受以下约束的控制:

不允许不安全块。

方法、运算符或访问器的参数不能是 ref 或 out。

yield
语句不能出现在匿名方法中。

当和 expression 一起使用时,yield return 语句不能出现在 catch 块中或含有一个或多个 catch 子句的 try 块中。

yield return 提供了迭代器一个比较重要的功能,即取到一个数据后马上返回该数据,不需要全部数据装入数列完毕,这样有效提高了遍历效率。

以下是一个比较特殊的例子:
using System;
using System.Collections;
using System.IO;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using System.Windows.Forms;
using System.Threading;

namespace test
{
    public class Persons : System.Collections.IEnumerable
    {
        #region IEnumerable 成员
        public System.Collections.IEnumerator GetEnumerator()
        {
            yield return "1";
            Thread.Sleep(5000);
            yield return "2";
            Thread.Sleep(5000);
            yield return "3";
            Thread.Sleep(5000);
            yield return "4";
            Thread.Sleep(5000);
            yield return "5";
            Thread.Sleep(5000);
            yield return "6";
        }
        #endregion
    }
    class program
    {
        static void Main()
        {
            Persons arrPersons = new Persons();
            foreach (string s in arrPersons)
            {
                System.Console.WriteLine(s);
            }
            System.Console.ReadLine();
        }
    }
}
每隔5秒钟,控制台就会输出一个数据,直到全部数据输入完毕。

12.4 C#迭代器在使用
foreach
语句时,需要在迭代的类中实现IEnumerable接口或ICollection接口。

这样做需要一个处理过程,从而变得有点麻烦。

在C#中,引入迭代器这个概念后,就很好地解决了上述问题。只要在C#项目程序的类或结构中实现迭代器,那么这个类和结构就支持foreach迭代,而不再需要实现全部的IEnumerable接口。当编译器检测到迭代器时,项目程序将自动实现
IEnumerable接口或实现IEnumerable接口的方法和属性。

12.4.1 迭代器概述

迭代器能够生成相同类型的有序代码块,通过上述代码块能够实现特定的处理功能。迭代器代码块和普通代码块类似,只是迭代器代码块里面存在不定量的yield语句。具体使用格式如下:
foreach语句表达式
{
     yield return
成员
;
…..
     yield break;
}

其中,yield return语句生成迭代的下一个值;yield break语句设置迭代终止完成。即关键字yield用于设置返回值,当程序达到yield return语句时,会保存当前的位置。当下次调用迭代器时,将从这个位置重新开始执行。
不用foreach , 直接用 IEnumerator 的方法实现迭代:如下
using System;
using System.Collections;
using System.IO;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using System.Windows.Forms;
using System.Threading;
namespace test
{
    public class Persons : System.Collections.IEnumerable
    {
        #region IEnumerable 成员
        private static Random m_rand = new Random();
        public System.Collections.IEnumerator GetEnumerator()
        {
            while (true)
            {
                yield return m_rand.Next(0,1000);
                Thread.Sleep(1000);
            }

        }
        #endregion
    }
    class program
    {
        static void Main()
        {
            Persons arrPersons = new Persons();
            IEnumerator array = arrPersons.GetEnumerator();
            while (array.MoveNext())
            {
                System.Console.WriteLine(array.Current.ToString());
            }
            System.Console.ReadLine();
        }
    }
*****************************************************************************************************************************
c#的 yield return看似一个可有可无的小功能,但实际上,是c#提供的支持函数式编程思想的重要组件之一。基于yield return,可以组合式的复用集合处理函数,同时只遍历一次集合,兼顾效率和灵活性。yield return 本身并不神秘,下面探讨其在c++下的实现方式。
考虑c#下的函数
        public static IEnumerable<int> CollectionFilter(IEnumerable<int> source)
        {
            PrevDo();
            foreach (var item in source)
            {
                GetItem(item);
                if (SomeCondition(item))
                {
                    PrevMatchItem();
                    yield return item;
                    PostMatchItem();
                }
            }
            PostDo();
        }
以下全为c++伪代码。为方便起见,不考虑释放对象。
class IEnumerable
{
public:
    virtual ~IEnumerable(){}
    virtual bool MoveNext()=0;
    virtual int Current()=0;
    virtual void Reset()=0;
};


将c#函数转换为c++伪代码
第一步,转换为带yield return的c++
        IEnumerable* CollectionFilter(IEnumerable* source)
        {
            PrevDo();
            source->Reset();
            while(source->MoveNext())
            {
                int item=source->Current();
                GetItem(item);
                if (SomeCondition(item))
                {
                    PrevMatchItem(item);
                    yield return item;
                    PostMatchItem(item);
                }
            }
            PostDo();
        }
第二步,用迭代器替换yield return
        IEnumerable* CollectionFilter(IEnumerable* source)
        {
            return new YieldReturnEnumerable(source);
        }       
第三步,实现迭代器
class YieldReturnEnumerable:public IEnumerable
{
public:
    YieldReturnEnumerable(IEnumerable* s)
    {
        _source=s;
        _first=true;
    }
    virtual bool MoveNext()
    {
        if(_first)
        {
            PrevDo();
            retEnum->Reset();
            _first=false;
        }
        else
        {
            PostMatchItem(source->Current());
        }
        bool findMatch=false;
        while(source->MoveNext())
        {
            int item=source->Current();
            GetItem(item);
            if (SomeCondition(item))
            {
                PrevMatchItem(item);
                findMatch=true;
                break;
            }
        }
        if(findMatch)
           return true;
        PostDo();
        return false;
    }
    virtual int Current()
    {
        return _source->Current();
    }
    virtual void Reset()
    {
        _source->Reset();
    }
private:
    IEnumerable* _source;
    bool _first;
};


其实就是iterator模式的一个应用。

原创粉丝点击