zoukankan      html  css  js  c++  java
  • C# Action 的一般用法

    class Program
         {
             static void Main(string[] args)
             {
                 string myName = "CC";
                 GetFullInfo(myName,s => Console.WriteLine(s));
     
                 Console.ReadLine();
                
             }
     
     
             static void GetFullInfo(string yourname, Action<string> action)
             {
                 string firstStr = "Welcome to cnblogs ";
                 action(firstStr + yourname);
             }
         }

    将不变的逻辑封装,变化的部分使用委托Action,增加了程序的灵活度!

    ----------------------------------------------------------------------

    public void ExecuteReader(SqlCommand cmd, Action<SqlDataReader> dataReaderCallBack)
            {
                using (var conn = GetConnection(_connectionString))
                {
                    cmd.PrepareCommand(conn);
                    
                        try
                        {
                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                dataReaderCallBack(reader);
                            }
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                }
            }

     xxx.ExecuteReader(termsCommand, reader =>
                {
                    while (reader.Read())
                    {
                        BasicTerm term = new BasicTerm();
                        term.ExternalName = reader.GetString("ExternalName");
                        term.ImageName = reader.GetString("FileName");
                        term.Title = reader.GetString("ShortText");
                        term.ReplacementToken = reader.GetString("ReplacementToken");
                        term.Text = reader.GetString("LongText");
                        term.ValueType = reader.GetString("ValueDataType");
                    }
                });

    这个是调用datareader的方法。
  • 相关阅读:
    Double 四舍五入保留小数
    QQ在线人数统计图数据解析
    Errors running builder 'Android Resource Manager' on project 'DeskClock'.
    批处理脚本学习笔记——程序猿版
    BZOJ 1002: [FJOI2007]轮状病毒
    《逆袭大学——传给IT学子的正能量》文件夹
    webservice 开发规范
    webservice面试题
    jdbc连接oracle数据库问题
    jdbc连接 orale 和 mysql 所需要的jar包
  • 原文地址:https://www.cnblogs.com/zcm123/p/2658287.html
Copyright © 2011-2022 走看看