12.字面值改进、out、ref和本地函数

12.CSharp各版本新功能和语法-CSharp7功能和语法-字面值outref弃元本地函数


12.1 知识点

C#7对应的Unity版本

  • Unity 2018.3支持C# 7
  • Unity 2019.4支持C# 7.3
  • 7.1, 7.2, 7.3相关内容都是基于 7的一些改进

C#7的新增功能和语法有哪些

  1. 字面值改进
  2. out 参数相关 和 弃元知识点
  3. ref 返回值
  4. 本地函数
  5. 抛出表达式
  6. 元组
  7. 模式匹配

字面值改进

  • 基本概念:在声明数值变量时,为了方便查看数值 可以在数值之间插入_作为分隔符
  • 主要作用:方便数值变量的阅读
int i1 = 9_9123_1239;
print(i1);//991231239

int i2 = 0xAB_CD_17;
print(i2);//11259159
  • 注意:不能再开头插入_

out变量的快捷使用 和 弃元

  • 用法:直接在out修饰的函数参数中声明变量,不需要再使用带有out参数的函数之前,声明对应变量
  • 作用:简化代码,提高开发效率
public void Calc(out int a, out int b)
{
    a = 10;
    b = 20;
}

public void Calc(out float a, out float b)
{
    a = 10;
    b = 20;
}

//1.以前使用带out函数的写法
//int a;
//int b;
//Calc(out a, out b);

//2.现在的写法
//就不需要前面声明x和y了
Calc(out int x, out int y);
print(x);
print(y);

//3.结合var类型更简便(但是这种写法在存在重载时不能正常使用,必须明确调用的是谁)
Calc(out int a, out var b);
print(a);
print(b);

//4.可以使用 _弃元符号 省略不想使用的参数
Calc(out int c, out _);
print(c);

ref修饰临时变量和返回值

  • 基本概念:使用ref修饰临时变量和函数返回值,可以让赋值变为引用传递
  • 作用:用于修改数据对象中的某些值类型变量
public struct TestRef
{
    public int atk;
    public int def;

    public TestRef(int atk, int def)
    {
        this.atk = atk;
        this.def = def;
    }
}

/// <summary>
/// 在给定的整数数组中查找指定数字,并返回其引用。
/// 如果找到了指定数字,则返回该数字在数组中的引用。
/// 如果未找到指定数字,则返回数组中的第一个元素的引用。
/// </summary>
/// <param name="numbers">整数数组</param>
/// <param name="number">要查找的数字</param>
/// <returns>找到的数字的引用</returns>
public ref int FindNumber(int[] numbers, int number)
{
    for (int i = 0; i < numbers.Length; i++)
    {
        if (numbers[i] == number)
            return ref numbers[i]; // 返回找到的数字的引用
    }
    return ref numbers[0]; // 返回数组中的第一个元素的引用
}

//1.修饰值类型临时变量
int testI = 100;  // 声明并初始化一个名为testI的整数变量,初始值为100
ref int testI2 = ref testI;  // 使用ref关键字创建一个testI的引用testI2,它们指向同一块内存
testI2 = 900;  // 修改testI2的值也会修改testI的值
print(testI);  // 输出900,testI也会跟着变化,相当于引用类型的特性

TestRef testRef1 = new TestRef(5, 5);  // 创建一个名为testRef1的TestRef对象,并指定atk和def属性的初始值
ref TestRef testRef2 = ref testRef1;  // 使用ref关键字创建一个testRef1的引用testRef2,它们指向同一个对象
testRef2.atk = 10;  // 修改testRef2的atk属性的值也会修改testRef1的atk属性的值
print(testRef1.atk);  // 输出10

//2.获取对象中的参数
ref int atk = ref testRef1.atk;  // 获取testRef1对象的atk属性的引用
atk = 99;  // 修改atk的值也会修改testRef1对象的atk属性的值
print(testRef1.atk);  // 输出99

//3.函数返回值
int[] numbers = new int[] { 1, 2, 3, 45, 5, 65, 4532, 12 };  // 声明并初始化一个整数数组numbers
ref int number = ref FindNumber(numbers, 5);  // 调用FindNumber函数返回在数组中找到的数字的引用
number = 98765;  // 修改number的值也会修改在数组中找到的数字的值
print(numbers[4]);  // 输出98765

本地函数

  • 基本概念:在函数内部声明一个临时函数
  • 注意:
    • 本地函数只能在声明该函数的函数内部使用
    • 本地函数可以使用父函数中的变量
  • 作用:方便逻辑的封装
  • 建议:把本地函数写在主要逻辑的后面,方便代码的查看
public int TestLocal(int i)
{
    bool b = false;
    i += 10;
    Calc();
    print(b);
    return i;
    
    //不需要修饰符
    void Calc()
    {
        i += 10;
        b = true;
    }
}

print(TestLocal(10)); //True 20

总结

  • C#7的新语法更新重点主要是代码简化
  • 今天学习的out和ref新用法,弃元、本地函数都是相对比较重要的内容
    -可以给我们带来很多便捷性

12.2 知识点代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

#region 知识点五 ref修饰临时变量和返回值

public struct TestRef
{
    public int atk;
    public int def;

    public TestRef(int atk, int def)
    {
        this.atk = atk;
        this.def = def;
    }
}

#endregion

public class Lesson12_CSharp各版本新功能和语法_CSharp7功能和语法_字面值改进out和ref新用法弃元本地函数 : MonoBehaviour
{
    void Start()
    {
        #region 知识点一 C#7对应的Unity版本
        //Unity 2018.3支持C# 7 
        //Unity 2019.4支持C# 7.3 
        //7.1, 7.2, 7.3相关内容都是基于 7的一些改进
        #endregion

        #region 知识点二 C#7的新增功能和语法有哪些
        //1.字面值改进
        //2.out 参数相关 和 弃元知识点
        //3.ref 返回值
        //4.本地函数

        //5.抛出表达式
        //6.元组
        //7.模式匹配
        #endregion

        #region 知识点三 字面值改进

        //基本概念:在声明数值变量时,为了方便查看数值 可以在数值之间插入_作为分隔符
        //主要作用:方便数值变量的阅读

        int i1 = 9_9123_1239;
        print(i1);//991231239

        int i2 = 0xAB_CD_17;
        print(i2);//11259159

        //注意:不能再开头插入_
        #endregion

        #region 知识点四 out变量的快捷使用 和 弃元

        //用法:不需要再使用带有out参数的函数之前,声明对应变量
        //作用:简化代码,提高开发效率

        //1.以前使用带out函数的写法
        //int a;
        //int b;
        //Calc(out a, out b);

        //2.现在的写法
        //就不需要前面声明x和y了
        Calc(out int x, out int y);
        print(x);
        print(y);

        //3.结合var类型更简便(但是这种写法在存在重载时不能正常使用,必须明确调用的是谁)
        Calc(out int a, out var b);
        print(a);
        print(b);

        //4.可以使用 _弃元符号 省略不想使用的参数
        Calc(out int c, out _);
        print(c);

        #endregion

        #region 知识点五 ref修饰临时变量和返回值

        //基本概念:使用ref修饰临时变量和函数返回值,可以让赋值变为引用传递
        //作用:用于修改数据对象中的某些值类型变量

        //1.修饰值类型临时变量
        int testI = 100;  // 声明并初始化一个名为testI的整数变量,初始值为100
        ref int testI2 = ref testI;  // 使用ref关键字创建一个testI的引用testI2,它们指向同一块内存
        testI2 = 900;  // 修改testI2的值也会修改testI的值
        print(testI);  // 输出900,testI也会跟着变化,相当于引用类型的特性

        TestRef testRef1 = new TestRef(5, 5);  // 创建一个名为testRef1的TestRef对象,并指定atk和def属性的初始值
        ref TestRef testRef2 = ref testRef1;  // 使用ref关键字创建一个testRef1的引用testRef2,它们指向同一个对象
        testRef2.atk = 10;  // 修改testRef2的atk属性的值也会修改testRef1的atk属性的值
        print(testRef1.atk);  // 输出10

        //2.获取对象中的参数
        ref int atk = ref testRef1.atk;  // 获取testRef1对象的atk属性的引用
        atk = 99;  // 修改atk的值也会修改testRef1对象的atk属性的值
        print(testRef1.atk);  // 输出99

        //3.函数返回值
        int[] numbers = new int[] { 1, 2, 3, 45, 5, 65, 4532, 12 };  // 声明并初始化一个整数数组numbers
        ref int number = ref FindNumber(numbers, 5);  // 调用FindNumber函数返回在数组中找到的数字的引用
        number = 98765;  // 修改number的值也会修改在数组中找到的数字的值
        print(numbers[4]);  // 输出98765


        #endregion

        #region 知识点六 本地函数
        
        //基本概念:在函数内部声明一个临时函数
        //注意:
        //本地函数只能在声明该函数的函数内部使用
        //本地函数可以使用声明自己的函数中的变量
        //作用:方便逻辑的封装
        //建议:把本地函数写在主要逻辑的后面,方便代码的查看

        print(TestLocal(10)); //True 20

        #endregion

        #region 总结
        //C#7的新语法更新重点主要是 代码简化
        //今天学习的out和ref新用法,弃元、本地函数都是相对比较重要的内容
        //可以给我们带来很多便捷性
        #endregion
    }

    #region 知识点四 out变量的快捷使用 和 弃元

    public void Calc(out int a, out int b)
    {
        a = 10;
        b = 20;
    }

    public void Calc(out float a, out float b)
    {
        a = 10;
        b = 20;
    }

    #endregion

    #region 知识点五 ref修饰临时变量和返回值

    /// <summary>
    /// 在给定的整数数组中查找指定数字,并返回其引用。
    /// 如果找到了指定数字,则返回该数字在数组中的引用。
    /// 如果未找到指定数字,则返回数组中的第一个元素的引用。
    /// </summary>
    /// <param name="numbers">整数数组</param>
    /// <param name="number">要查找的数字</param>
    /// <returns>找到的数字的引用</returns>
    public ref int FindNumber(int[] numbers, int number)
    {
        for (int i = 0; i < numbers.Length; i++)
        {
            if (numbers[i] == number)
                return ref numbers[i]; // 返回找到的数字的引用
        }
        return ref numbers[0]; // 返回数组中的第一个元素的引用
    }


    #endregion

    #region 知识点六 本地函数


    public int TestLocal(int i)
    {
        bool b = false;  // 声明并初始化一个名为b的布尔变量,初始值为false
        i += 10;  // 将i的值增加10
        Calc();  // 调用Calc()本地函数
        print(b);  // 输出b的值
        return i;  // 返回i的值

        //不需要修饰符
        void Calc()
        {
            i += 10;  // 将i的值增加10
            b = true;  // 将b的值设为true
        }
    }


    #endregion
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏