看仓储模式,有代码写到这几个关键字,陌生,记录下来。

      定义一个类型,此类型抽象化了相似结构的某一类方法,因此我们能将此类型代表的方法作为参数进行传递。

     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() 知识点的更多相关文章

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

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

  2. 委托、多播委托、泛型委托Func,Action,Predicate,ExpressionTree

    当试图通过一个事件触发多个方法,抽象出泛型行为的时候,或许可以考虑使用委托.     通过委托构造函数或委托变量把方法赋值给委托 private delegate double DiscountDel ...

  3. delegate Func Action Expression

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; na ...

  4. C#の----Func,Action,predicate在WPF中的应用

    首先介绍下,winform中可以用this.invoke来实现:wpf中要使用调度器Control.Despite.invoke: (Action)(()=> { })和 new Action ...

  5. C# delegate event func action 匿名方法 lambda表达式

    delegate event action func 匿名方法 lambda表达式 delegate类似c++的函数指针,但是是类型安全的,可以指向多个函数, public delegate void ...

  6. C#中匿名函数、委托delegate和Action、Func、Expression、还有Lambda的关系和区别

    以前一直迷迷糊糊的,现在总算搞明白. Lambda表达式 Lamda表达式基本写法是()=>{ };Lambda和方法一样都可以传入参数和拥有返回值.(int x)=>{return x; ...

  7. C#的泛型委托Predicate/Func/Action

    Predicate<T> 是一个委托,它代表了一个方法,它的定义是: namespace System {    // 摘要:    表示定义一组条件并确定指定对象是否符合这些条件的方法. ...

  8. 系统内置委托:Func/Action

    lSystem.Func 代表有返回类型的委托 lpublic delegate TResult  Func<out TResult>(); lpublic delegate TResul ...

  9. C#基础-Func,Action

    Func,Action 的介绍及其用法 Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate,Func位于System.Core命名空间下,使用委托可以提升效率,例如 ...

随机推荐

  1. NodeJs中require use get typescript及其他知识点集合

    NodeJs的Express使用 nodejs事件的监听与事件的触发 TypeScript学习笔记 深入浅出Node.js Nodejs开发Office插件 类百度文库文档上传.转换和展示功能项目开源 ...

  2. leetcode384

    public class Solution { private int[] nums; private Random random; public Solution(int[] nums) { thi ...

  3. maven-resources-plugin使用

    命令行中带参数指定${}变量值 <build> <resources> <resource> <directory>src/main/resources ...

  4. VS2015编译VLC2.2.1(under WIN7-64)<转>

    概述: 感谢https://github.com/sunqueen/vlc-2.2.1.32-2013 这个工程,我的工作基本上都是基于它,我只是觉得他的工程设置不够清晰,重新做了一次.区别在于我的工 ...

  5. ffmpeg问题汇总及解决方案 <设置avformat_open_input 超时><转>

    1:如果数据是rtp/rtsp传输的话,ffmpeg会每隔30s(哪里设置该值?)发送一个keepalive包,如果ipc支持GET_PARAMETER命令,就发该命令等ipc回复以确认ipc还活着. ...

  6. 一个简单的环境光shader

    关于shader的一个简短的历史 在DirectX8之前,GPU有一个固定的方法去变换顶点和像素,称为“固定管线”.这使得在将它们传递给GPU后,开发者不可能操作顶点和像素的变换. DirectX8介 ...

  7. [hdu4347]The Closest M Points(平衡树式kdtree)

    解题关键:模板题(结合起来了) #include<iostream> #include<cstdio> #include<cstring> #include< ...

  8. 保持在Div 底部的方法

    <!DOCTYPE> <html> <head> <meta http-equiv="content-type" content=&quo ...

  9. 第一个Django应用程序_part1

    一.查看Django是否安装 参考文档:https://docs.djangoproject.com/en/1.11/intro/tutorial01/ 如果Django已经安装,可以看到安装的版本号 ...

  10. Python学习笔记_读Excel去重

    读取一个Excel文件,按照某列关键字,如果有重复则去掉 这里不介绍所有的解决办法,只是列出一个办法. 软件环境: OS:Win10 64位 Python 3.7 测试路径:D:\Work\Pytho ...