0
点赞
收藏
分享

微信扫一扫

C#常见技能_结构

琛彤麻麻 2023-07-13 阅读 37

变量:

无符号
byte正8位  ushort正16位  uint正32位  ulong正64位
有符号
sbyte8位  short16位  int32位  long64位
浮点数
float double decimal
特殊
char bool string

复杂数据容器:

枚举enum
结构体struct
数组(一维、二维、交错) [] [,] [][]

数据集合:

using System.Collections;
ArrayList object数据列表
stack 栈 先进后出
Queue 队列 先进先出
Hashtable 哈希表 键值对

泛型数据集合:

using system.Collections.Generic;
List 列表 泛型列表
Dictionary 字典 泛型哈希表
LinkedList 双向链表
statck 泛型栈
Queue 泛型队列

2.委托

委托是函数(方法)的容器
可以理解为表示函数(方法)的变量类型
用来存储、传递函数(方法)
委托的本质是一个类,用来定义函数(方法)的类型(返回值和参数的类型)
不同的函数(方法)必须对应和各自"格式"一致的委托

关键字:delegate

语法:访问修饰符delegate返回值委托名(参数列表);

可以申明在namespace和class语句块中
更多的写在namespace中
简单记忆委托语法就是函数申明语法前面加一个delegate关键字

声明:

 /// <summary>
    /// 声明了一个可以存储无参无返回值的函数容器
    /// </summary>
    delegate void MyFun();

    //委托声明不能重名(同一语句块)    
    delegate int MyFunc(int a);

使用:

public class test : MonoBehaviour
{    
    /// <summary>
    /// 使用过程
    /// </summary>
    private void Start()
    {
        //无参无返回值委托使用
        MyFun mf = new MyFun(Fun);
        mf.Invoke();
        mf();
        //有参有返回值委托使用
        MyFunc mfc = new MyFunc(Fun2);
        mfc.Invoke(1);
        mfc(2);

        Tet tet = new Tet();
        tet.TestFun(Fun, Fun2);
    }  
    void Fun()
    {
        Debug.Log("fun");
    }
    int Fun2(int a)
    {
        Debug.Log("fun2" + a);
        return a;
    }
}
/// <summary>
/// 声明了一个可以存储无参无返回值的函数容器
/// </summary>
delegate void MyFun();

//委托声明不能重名(同一语句块)    
delegate int MyFunc(int a);

// 委托常用在:
//1.作为类的成员
//2.作为函数的参数
class Tet
{
    public MyFun mf;
    public MyFunc mfc;

    public void TestFun(MyFun mf,MyFunc mfc)
    {
        //可以先处理一些别的逻辑,完事后在执行函数
        this.mf = mf;
        this.mfc = mfc;
    }
}

多函数储存:

public class test : MonoBehaviour
{    
    /// <summary>
    /// 使用过程
    /// </summary>
    private void Start()
    {
        //委托存储多个函数
        //增
        MyFun mf = Fun;
        mf += Fun2;        
        //减(多减不会报错)
        mf -= Fun2;
        //清空容器
        mf = null;
    }  
    void Fun()
    {
        Debug.Log("fun");
    }
    void Fun2()
    {
        Debug.Log("fun2");
    }
}

delegate void MyFun();

系统定义好的委托:

public class test : MonoBehaviour
{    
    /// <summary>
    /// 使用过程
    /// </summary>
    private void Start()
    {
        //无参无返回值
        Action action = Fun;
        action += Fun2;
        action();

        //泛型委托
        Func<string> FuncStr = Func3;

        //可传n参委托
        Action<int, string> action1 = Fun4;
        //可传n参委托。有返回值
        Func<int,int> FuncStr2 = Fun5;
    }
    void Fun(){ Debug.Log("fun"); }
    void Fun2(){ Debug.Log("fun2"); }
    string Func3(){ return ""; }
    void Fun4(int a ,string b) { }
    int  Fun5(int a){ return 0; }
}

delegate void MyFun();

3.事件

事件是什么:
事件是基于委托的存在
事件是委托的安全包裹
让委托的使用更具有安全性
事件 是一种特殊的变量类型

二事件的使用
1.事件是作为 成员变量存在于类中
2.委托怎么用 事件就怎么用

事件相对于委托的区别:
1.不能在类外部 赋值
2.不能再类外部 调用

注意:
它只能作为成员存在于类和接口以及结构体中

public class test : MonoBehaviour
{    
    
    private void Start()
    {
        Tes tes = new Tes();
        tes.myFun = null;
        tes.myFun = TestMyFun;

        //事件不可以在类外部赋值
        //tes.myEvent = null;
        //tes.myEvent = TestMyFun;
        //但是事件可以在外部+=、-=
        tes.myEvent += TestMyFun;
        tes.myEvent -= TestMyFun;

        tes.myFun();
        tes.myFun.Invoke();
        //事件不能在外部调用
        //tes.myEvent();
        //tes.myEvent.Invoke();
        //只能在类内部封装方法去调用
    }
    void TestMyFun() { }
}
class Tes
{
    //委托成员,存储函数
    public Action myFun;
    //事件成员,存储函数
    public event Action myEvent;

    //构造函数
    public Tes(){
        //事件的使用和委托一样
        myFun = TestFun;
        myFun += TestFun;
        myFun -= TestFun;
        myFun();

        myEvent = TestFun;
        myEvent += TestFun;
        myEvent -= TestFun;
        myEvent();
    }

    public void TestFun()
    {
        Debug.Log("122");
    }
}

事件相当于对委托进行了一次封装,更安全。

public class test : MonoBehaviour
{    
    
    private void Start()
    {
        Heater ht = new Heater();
        Alarm alarm = new Alarm();
        Display display = new Display();

        ht.HeaterEvent += alarm.OnAlert;
        ht.HeaterEvent += display.OnDisplay;
        ht.Controll();
    }
}
public class Heater
{
    public delegate void HeaterDelegate(int alert);
    public event HeaterDelegate HeaterEvent;

    private int time;
    public void Controll()
    {
        for (int i = 0; i <= 100; i++)
        {
            time = i;
            if(time > 95)
            {
                if(HeaterEvent != null)
                {
                    HeaterEvent(time);
                }
            }
        }
    }
}

public class Alarm
{
    public void OnAlert(int alert)
    {
        Debug.Log("alarm:已经"+ alert + "度了");
    }
}
public class Display
{
    public void OnDisplay(int alert)
    {
        Debug.Log("Display:烧到" + alert + "度了,可以褪猪毛啦");
    }
}
举报

相关推荐

c#结构

C#,.NET常见算法

C# 结构体

C# 结构(Struct)

C#中的结构

C# 程序结构

0 条评论