Func<>用法
Func是一个委托,委托里面可以存方法,Func<string,string>或Func<string,string,int,string>等
前几个是输入参数,最后一个是返回参数
以Func<int,bool>为例:
private bool IsNumberLessThen5(int number)
{return number < 5;}
Func<int,bool> f1 = IsNUmberLessThen5;
调用:
bool flag= f1(4);
//以下是其它的用法,与IsNumberLessThen5作用相同。只是写法不同而已。
Func<int, bool> f2 = i => i < 5;
Func<int, bool> f3 = (i) => { return i < 5; };
Func<int, bool> f4 = delegate(int i) { return i < 5; };
多参数的使用方法
Func<int,int ,int> Coun = (x,y) =>
{
return x + y;
};
自己项目中实例
类库的一个方法需要用到Server.MapPath返回的路径,但是类库中无法直接使用Server.MapPath方法
于是在web中定义一个方法,接收一个相对路径字符串,返回相对于根目录的字符串
Func<string, string> getphysicsPath = p =>
{
return Server.MapPath(p);
};
在类库的中:
public class SaveUploadFile
{
public static string SaveImage(HttpPostedFileBase file, Func<string, string> getPath)
{ DateTime now = DateTime.Now;
string newFileName = string.Empty;
string fileType = Path.GetExtension(file.FileName);
string suijishu = Math.Abs(Guid.NewGuid().GetHashCode()).ToString();
newFileName = now.ToString("yyyyMMddHHmmss") + "_" + suijishu + fileType; string path = CommConfig.UploadImageUrl + "/" + now.ToString("yyyyMMdd"); string physicsPath = getPath(path);//调用传入的方法计算路径 string fullPath = physicsPath + "/" + newFileName;
string newFilePath = path + "/" + newFileName; if (!Directory.Exists(physicsPath))
{
Directory.CreateDirectory(physicsPath);
} file.SaveAs(fullPath); return newFilePath;
}
}
最后在web中
var file = Request.Files[0]; string newFilePath= SaveUploadFile.SaveImage(file, getphysicsPath);

Func<>用法的更多相关文章
- MVC ---- 理解学习Func用法
//Func用法 public static class FuncDemo{ public static void TestFunc(){ //数据源 List<User> usList ...
- Action与Func 用法
//vs2017 + framework4.6.2 //zip https://github.com/chxl800/ActionFuncDemo //源文件git https://gith ...
- action func用法记记
public partial class Form1 : Form { public Form1() { InitializeComponent(); } public delegate void s ...
- 委托小结及Func用法
首先,委托是一种类型,由关键字delegate声明.确切的说,委托是一种可用于封装命名或者匿名方法的引用类型. 它类似于 C++ 中的函数指针,而且是类型安全和可靠的. 委托类型的声明与 ...
- c# 委托(Func、Action)
以前自己写委托都用 delegate, 最近看组里的大佬们都用 Func , 以及 Action 来实现, 代码简洁了不少, 但是看得我晕晕乎乎. 花点时间研究一下,记录一下,以便后期的查阅. 1.F ...
- python note 11 函数名的使用、闭包、迭代器
1.函数名就是一个变量 def func(): print("我是一个小小的函数") a = func print(a) #输出变量存放地址 <function func a ...
- Python内建函数一
内建函数 1. abs(number) 用法:返回数字的绝对值 2. all(iterable) 用法:如果iterable的所有元素都是真值,就返回True,否则返回False 3. any(ite ...
- golang中type关键字使用
type关键字使用 type是go语法里的重要而且常用的关键字,type绝不只是对应于C/C++中的typedef.搞清楚type的使用,就容易理解go语言中的核心概念struct.interface ...
- C#中Predicate<T>与Func<T, bool>泛型委托的用法实例
本文以实例形式分析了C#中Predicate<T>与Func<T, bool>泛型委托的用法,分享给大家供大家参考之用.具体如下: 先来看看下面的例子: 1 2 3 4 5 6 ...
随机推荐
- 解决Java保存到数据库中文乱码问题,加useUnicode=true&characterEncoding=UTF-8
Java保存到数据库中文乱码, 解决方法如下: 我们在连接MySQL数据库的时候一般都会在url后面添加useUnicode=true&characterEncoding=UTF-8,但是问什 ...
- Eclipse中英文对照表(整理笔记)
Eclipse百度界面中英文对照 目录 Eclipse百度界面中英文对照 0.菜单栏 1.File 文件菜单 2.Edit 编辑菜单 3.Source 源代码 4.Refactor 重构 5.Navi ...
- 编写Java程序,创建一个 Person 类,该类中有一个类成员变量 country、一个实例变量 name 和另一个实例变量 age。
返回本章节 返回作业目录 需求说明: 创建一个 Person 类,该类中有一个类成员变量 country.一个实例变量 name 和另一个实例变量 age. country 表示地区,name 表示姓 ...
- 初识python 之 爬虫:BeautifulSoup 的 find、find_all、select 方法
from bs4 import BeautifulSoup lxml 以lxml形式解析html,例:BeautifulSoup(html,'lxml') # 注:html5lib 容错率最高fin ...
- spring-aop(二)学习笔记
常用增强处理类型 增强处理类型 特点 before 前置增强处理,在目标方法前织入增强处理 ...
- 第10组 Alpha冲刺 (2/6)
1.1基本情况 ·队名:今晚不睡觉 ·组长博客:https://www.cnblogs.com/cpandbb/ ·作业博客:https://edu.cnblogs.com/campus/fzu/FZ ...
- PPT2010制作图片玻璃磨砂效果
原文链接: https://www.toutiao.com/i6488928834799272462/ 选择"插入"选项卡,"图像"功能组,"图片&q ...
- SYCOJ1793
题目-统计单词前缀数 (shiyancang.cn) 1 #include<bits/stdc++.h> 2 using namespace std; 3 map<string,in ...
- C++ 从&到&&
人类发展史,就是不断挖坑.填坑的过程. 语言发展史也是如此! 任何一门设计合理的语言,给你的限制或提供的什么特性,都不是没有代价的. C的指针 指针:pointer 指针的思想起源于汇编.指针思想是编 ...
- 《剑指offer》面试题20. 表示数值的字符串
问题描述 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100"."5e2"."-123"."3.1 ...