using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace FuncDemo
{
class Program
{
static void Main(string[] args)
{
//类似委托功能
Func<string, int> test = TsetMothod; Console.WriteLine(test("")); Func<string, int> test1 = TsetMothod; //只需要调用这个类就可以减少重复的代码
CallMethod<string>(test1,""); //或者采用这种
CallMethod<string>(new Func<string, int>(TsetMothod), ""); CallMethod(new Func<string, int>(TsetMothod), ""); } public static int TsetMothod(string name)
{
if (string.IsNullOrEmpty(name))
{
return ;
}
return ; } //改造经常出现类似代码下面 //try
//{
////Do();只有这个方法改变
//}
//catch (System.Exception ex)
//{
// //Log(e);
//}
//finally
//{
////DoOther();
//} //
public static void CallMethod<T>(Func<T,int> func,T item)
{
try
{
int i= func(item);
Console.WriteLine(i);
}
catch(Exception e)
{ }
finally
{ }
} }
}FUNC示例

C# FUNC 应用的更多相关文章

  1. C#基础知识六之委托(delegate、Action、Func、predicate)

    1. 什么是委托 官方解释 委托是定义方法签名的类型,当实例化委托时,您可以将其实例化与任何具有兼容签名的方法想关联,可以通过委托实例调用方法. 个人理解 委托通俗一点说就是把一件事情交给别人来帮助完 ...

  2. C#委托的介绍(delegate、Action、Func、predicate)

    委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明   Deleg ...

  3. winform总结2> Action<> ,Action,func<>,委托相关的理解

    1,他们是什么: Action 封装一个方法,该方法不具有参数并且不返回值. Action<> 封装一个方法,该方法具有最多16个参数并且不返回值. func<> 封装一个具有 ...

  4. Func

    Func<List<int>, string> getStr = (list) => { var returnStr = ""; if (list.A ...

  5. 浅谈C#中常见的委托<Func,Action,Predicate>(转)

    一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...

  6. .NET Core中合并Expression<Func<T,bool>>的正确姿势

    这是在昨天的 .NET Core 迁移中遇到的问题,之前在 .NET Framework 中是这样合并 Expression<Func<T,bool>> 的: public s ...

  7. Delegate、Predicate、Action和Func

    写在前面 Delegate Predicate Action Func 逆变和协变 先说下什么是委托(Delegate),委托在C#中是一种类型,和Class是一个级别,但是我们经常把它看做是一个方法 ...

  8. VS2012 Unit Test(Void, Action, Func) —— 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试

    [提示] 1. 阅读文本前希望您具备如下知识:了解单元测试,了解Dynamic,熟悉泛型(协变与逆变)和Lambda,熟悉.NET Framework提供的 Action与Func委托.2.如果您对单 ...

  9. C#委托的介绍(delegate、Action、Func、predicate) --转载

    来源:http://www.cnblogs.com/akwwl/p/3232679.html 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1 ...

  10. EXPLAINING WHAT ACTION AND FUNC ARE

    http://simpleprogrammer.com/2010/09/24/explaining-what-action-and-func-are/ Explaining What Action A ...

随机推荐

  1. Opencv2.4.9安装和在visualstudio 2013中配置

    Opencv2.4.9安装和在visualstudio 2013中配置 下载opencv和在windows下安装: 最新版本号的opencv是2014.4.25的opencv2.4.9,这里选择当前最 ...

  2. 基于Teigha.Net实现CAD到SHP的转换方案

    CAD在测绘领域运用广泛,所以,现在有很多成果都是CAD格式,但其自身存在很多局限性,需将其转为支持更加广泛,存储更加完善的 SHP文件.ArcGIS中直接提供相关转换工具,但不能转换Xdata,Ar ...

  3. Android 之开发积累

    1.后台设置ImageView的src属性 有三种方式:img = (ImageView)this.findViewById(R.id.img_result_analyze); [1]setImage ...

  4. [算法][LeetCode]Spiral Matrix

    题目要求 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spir ...

  5. cocos2d 重写顶点着色语言

    bool CCShaderSprite::initWithFile( const char *pszFilename ) {  bool ret=false;  do  {    ret=CCSpri ...

  6. java String字符串

      五.java数据类型之String(字符串) CreateTime--2017年7月21日16:17:45 Author:Marydon (一)数据格式 (二)初始化 // 方式一 String ...

  7. 以__name__进行单元测试

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #以__name__进行单元测试 #何为单元测试? #单元测试(模块测试)是开发者编写的一小段代码,用于检测被 ...

  8. 在notepad++中运行python代码

    #在notepad++中运行python代码 ''' 1.安装插件pyNPP, 2.允许插件pyNPP中的第一个和第二个选项即可,如果代码过少代码执行一闪而过,可能无法看到,可加入少量sleep时间即 ...

  9. ls -lrt

    1,按照时间升序 命令:ls -lrt 详细解释: -l use a long listing format 以长列表方式显示(详细信息方式) -t sort by modification time ...

  10. 【微信小程序】详解wx:if elif else的用法(搭配view、block)

    1.搭配view <view wx:if="{{boolean==true}}"> <view class="bg_black">< ...