c#查漏2重点篇章

1.unity自带的Action?

2.

C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针。委托(Delegate) 是存有对某个方法的引用的一种引用类型变量。引用可在运行时被改变。

委托(Delegate)特别用于实现事件和回调方法。所有的委托(Delegate)都派生自 System.Delegate 类。

3 委托的多播

using System;
using System.IO;

namespace DelegateAppl
{
class PrintString
{
static FileStream fs;
static StreamWriter sw;
// 委托声明
public delegate void printString(string s);

// 该方法打印到控制台
public static void WriteToScreen(string str)
{
Console.WriteLine("The String is: {0}", str);
}
// 该方法打印到文件
public static void WriteToFile(string s)
{
fs = new FileStream("c:\\message.txt",
FileMode.Append, FileAccess.Write);
sw = new StreamWriter(fs);
sw.WriteLine(s);
sw.Flush();
sw.Close();
fs.Close();
}
// 该方法把委托作为参数,并使用它调用方法
public static void sendString(printString ps)
{
ps("Hello World");
}
static void Main(string[] args)
{
printString ps1 = new printString(WriteToScreen);
printString ps2 = new printString(WriteToFile);
sendString(ps1);
sendString(ps2);
Console.ReadKey();
}
}
}

委托没有static

public class demo : MonoBehaviour
{

public delegate void my();

// Use this for initialization
void Start()
{
my ha = new my(totto);
ha += ssk;
go(ha);
}

public void totto()
{
Debug.Log("totto");
}
public static void ssk()
{
Debug.Log("ssk");
}

public static void go(my sk)
{
if (sk != null)
{
sk();
}
}


}

猜你喜欢

转载自www.cnblogs.com/xiaomao21/p/9175271.html