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命名空间下,使用委托可以提升效率,例如 ...
随机推荐
- DDoS攻防战 (四):CC攻击防御系统部署
1. 系统效果 此DDOS应用层防御系统已经部署在了http://www.yfdc.org网站上(如果访问失败,请直接访问位于国内的服务器http://121.42.45.55进行在线测试). 此防御 ...
- 从socket开始讲IOS网络编程
home list tags talk user rss Mac&iOS Socket 大纲 一.Socket简介 二.BSD Socket编程准备 1.地址 2.端口 3.网络字节序 4.半 ...
- input 隐藏边框
style='border-left:0px;border-top:0px;border-right:0px;border-bottom:1px; border-bottom-color:Black'
- CSS选择器的匹配规则
css选择器是从右向左匹配的, 比如:.list a {color:blue;} 先解析到 a 标签,并将页面上所有 a 标签的字体颜色都按照 color:blue 进行渲染(蓝色),再解析到 .li ...
- 纸牌游戏CardBattle的设计与开发
- java常用正则校验工具类
正则常用校验工具类 import java.util.regex.Pattern; /** * @program: * @description: 校验工具类 * @author: xujingyan ...
- DevCloud for CloudStack Development
Apache CloudStack development is not an easy task, for the simplest of deployments one requires a se ...
- Linux ssldump命令
一.简介 tcpdump是一款很强大.很有用的网络侦听软件,但是对于ssl加密的数据包就无能为力了:ssldump则是一款可以侦听ssl加密的数据包的软件. 二.安装 1)通过yum安装 yum ...
- [C++] Vtable(虚函数表)
Vtable(虚函数表)
- [C++] c pointer
the nature of pointer const keyword const int* p int const *p int* const p int const a const int a ...