Delegate Func Action Predicate default() 知识点
看仓储模式,有代码写到这几个关键字,陌生,记录下来。
定义一个类型,此类型抽象化了相似结构的某一类方法,因此我们能将此类型代表的方法作为参数进行传递。
Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型
Func可以接受0个至16个传入参数,必须具有返回值 用于泛型
Action可以接受0个至16个传入参数,无返回值 用于泛型
Predicate只能接受一个传入参数,返回值为bool类型 用于泛型
default(T) 针对泛型默认值的初始化处理。 当T不确认是值类型或引用类型时,他的默认值会给我们造成困扰。既可以是NULL又可能是0。当使用default(T)处理后T temp = default(T);
temp的默认值会根据T的类型自动赋值。
//下面代码来自msdn.microsoft.com
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Test with a non-empty list of integers.
GenericList<int> gll = new GenericList<int>();
gll.AddNode();
gll.AddNode();
gll.AddNode();
int intVal = gll.GetLast();
// The following line displays 5. 结果将返回5
System.Console.WriteLine(intVal); // Test with an empty list of integers.用空值进行测试
GenericList<int> gll2 = new GenericList<int>();
intVal = gll2.GetLast();
// The following line displays 0. 结果返回0
//此处根据T的类型返回了int型的默认值0
System.Console.WriteLine(intVal); // Test with a non-empty list of strings.
GenericList<string> gll3 = new GenericList<string>();
gll3.AddNode("five");
gll3.AddNode("four");
string sVal = gll3.GetLast();
// The following line displays five.
System.Console.WriteLine(sVal); // Test with an empty list of strings.
GenericList<string> gll4 = new GenericList<string>();
sVal = gll4.GetLast();
// The following line displays a blank line.
//此处根据T的类型返回string的默认值“空白”
System.Console.WriteLine(sVal);
}
} // T is the type of data stored in a particular instance of GenericList.
public class GenericList<T>
{
private class Node
{
// Each node has a reference to the next node in the list.
public Node Next;
// Each node holds a value of type T.
public T Data;
} // The list is initially empty.
private Node head = null; // Add a node at the beginning of the list with t as its data value.
public void AddNode(T t)
{
Node newNode = new Node();
newNode.Next = head;
newNode.Data = t;
head = newNode;
} // The following method returns the data value stored in the last node in
// the list. If the list is empty, the default value for type T is
// returned.
public T GetLast()
{
// The value of temp is returned as the value of the method.
// The following declaration initializes temp to the appropriate
// default value for type T. The default value is returned if the
// list is empty.
T temp = default(T); Node current = head;
while (current != null)
{
temp = current.Data;
current = current.Next;
}
return temp;
}
}
}
Delegate Func Action Predicate default() 知识点的更多相关文章
- 浅谈C#中常见的委托<Func,Action,Predicate>(转)
一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...
- 委托、多播委托、泛型委托Func,Action,Predicate,ExpressionTree
当试图通过一个事件触发多个方法,抽象出泛型行为的时候,或许可以考虑使用委托. 通过委托构造函数或委托变量把方法赋值给委托 private delegate double DiscountDel ...
- delegate Func Action Expression
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; na ...
- C#の----Func,Action,predicate在WPF中的应用
首先介绍下,winform中可以用this.invoke来实现:wpf中要使用调度器Control.Despite.invoke: (Action)(()=> { })和 new Action ...
- C# delegate event func action 匿名方法 lambda表达式
delegate event action func 匿名方法 lambda表达式 delegate类似c++的函数指针,但是是类型安全的,可以指向多个函数, public delegate void ...
- C#中匿名函数、委托delegate和Action、Func、Expression、还有Lambda的关系和区别
以前一直迷迷糊糊的,现在总算搞明白. Lambda表达式 Lamda表达式基本写法是()=>{ };Lambda和方法一样都可以传入参数和拥有返回值.(int x)=>{return x; ...
- C#的泛型委托Predicate/Func/Action
Predicate<T> 是一个委托,它代表了一个方法,它的定义是: namespace System { // 摘要: 表示定义一组条件并确定指定对象是否符合这些条件的方法. ...
- 系统内置委托:Func/Action
lSystem.Func 代表有返回类型的委托 lpublic delegate TResult Func<out TResult>(); lpublic delegate TResul ...
- C#基础-Func,Action
Func,Action 的介绍及其用法 Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate,Func位于System.Core命名空间下,使用委托可以提升效率,例如 ...
随机推荐
- leetcode840
本题不清楚题意,从网上找到了python的解答,记录如下. class Solution: def numMagicSquaresInside(self, grid): ans, lrc = 0, [ ...
- cas-client单点登录客户端拦截请求和忽略/排除不需要拦截的请求URL的问题
http://blog.csdn.net/eguid_1/article/details/73611781
- dbcm with kubenetes
1. create consul # kcompose convert -f /root/gitSwarm/dbcm-base-managers/compose/consul.yml genetate ...
- VMWare windows找不到microsoft软件许可条款
提示如下错误: windows找不到microsoft软件许可条款.请确保安装源有效,然后重新启动安装. 解决方案: 把该虚拟机中的系统硬件配置中的软盘去掉. 程序员的基础教程:菜鸟程序员
- 美化input type=range标签滑动样式(带渐变效果)
input原来的样式就不在此赘述了: 下面看一下实际项目中用到的input输入框,同步绑定输入数据,实现输入框双向绑定(实际项目中使用的是vue框架): html部分: <div class=& ...
- python 全栈基础作业题
1.执行 Python 脚本的两种方式 1..直接使用PyCharm执行 2.python run.py 调用python 解释器来调用python脚本 2.简述位.字节的关系 数据存储是以“字节”( ...
- p4042 [AHOI2014/JSOI2014]骑士游戏
传送门 分析 我们发现对于一个怪物要不然用魔法代价使其无需考虑后续点要么用普通攻击使其转移到他所连的所有点上且所有边大于0 所以我们可以先将一个点的最优代价设为魔法攻击的代价 之后我们倒着跑spfa求 ...
- 3d点云与cad模型
https://stackoverflow.com/questions/19000096/match-3d-point-cloud-to-cad-model
- Docker 实现的 redis 主从
计划用 Docker 实现 Redis 的主从,简单主从而已.主的名称叫 redis-master 一步步来. 先新建个Dockerfile ,从alpine 开始,比较简单. FROM alpine ...
- js失效的原因及解决方式
1.在head中先引用了js文件再引用jquery,应先引用jquery 2.js文件中所有代码应包含在$(function(){ });中