Unity3D-学习记录之参数修饰符——ref 、out、params
目录
1、博客介绍
日常学习记录,参数修饰符ref 、out、params的一些简单用法
2、内容
(1)params
params作为参数修饰可以传入不定数量的参数
(2)ref
ref作为参数修饰符 将参数作为引用类型传入,方法内修改传入参数同样会修改原属性,使用ref修饰符,可以在方法内不进行修饰
(3) out
out作为参数修饰符 将参数作为引用类型传入,方法内修改传入参数同样会修改原属性,使用out修饰符,在方法内必须进行修饰
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// time:2019/3/4
/// author:Sun
/// des:参数修饰符
/// path:https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/params
/// </summary>
public class Test : MonoBehaviour
{
#region ------------params参数修饰符------------
/// <summary>
/// params作为参数修饰可以传入不定数量的参数
/// </summary>
/// <param name="numArrary"></param> 必须用一位数组来表示
private void FucParams(params int[] numArrary)
{
for (int i = 0; i < numArrary.Length; i++)
{
print(numArrary[i]);
}
}
/// <summary>
/// param修饰的参数必须放在最后以为
/// </summary>
/// <param name="str"></param>
/// <param name="numArrary"></param>
private void FucParams1(string str,params int[] numArrary)
{
print(str);
for (int i = 0; i < numArrary.Length; i++)
{
print(numArrary[i]);
}
}
#endregion------------------------------------
#region --------------in参数修饰符--------------
//C# 7.2新特性
#endregion------------------------------------
#region --------------ref参数修饰符--------------
/// <summary>
/// 初始化
/// ref作为参数修饰符 将参数作为引用类型传入,方法内修改传入参数同样会修改原属性
/// </summary>
private int refNum = 10;
private void FucRef(ref int num)
{
print("初始化写入前"+refNum);
num = 100;
print("初始化写入后"+refNum);
}
/// <summary>
/// 为初始化
/// </summary>
private int refNum1;
private void FucRef1(ref int num)
{
print("未初始化写入前"+refNum1);
num = 100;
print("未初始化写入后"+refNum1);
}
private void FucRef2(ref int num)
{
print("使用ref修饰符,可以在方法内不进行修饰");
}
#endregion------------------------------------
#region --------------out参数修饰符--------------
/// <summary>
/// 初始化
/// </summary>
private int outNum = 10;
private void FucOut(out int num)
{
print("初始化写入前"+outNum);
num = 100;
print("初始化写入后"+outNum);
}
/// <summary>
/// 为初始化
/// </summary>
private int outNum1;
private void FucOut1(out int num)
{
print("未初始化写入前"+outNum1);
num = 100;
print("未初始化写入后"+outNum1);
}
private void FucOut2(out int num)
{
num = 101;
print("使用out修饰符,可以在方法内必须修饰");
}
#endregion------------------------------------
private void OnGUI()
{
if (GUI.Button(new Rect(100,100,100,100),"params修饰符" ))
{
FucParams(1,2,3,4,5);
print("---------分割线--------");
FucParams1("我是字符串",1,2,3);
print("---------分割线--------");
}
if (GUI.Button(new Rect(200,100,100,100),"in修饰符" ))
{
print("-------空着-------");
}
if (GUI.Button(new Rect(300,100,100,100),"ref修饰符" ))
{
print("---------分割线--------");
FucRef(ref refNum);
FucRef1(ref refNum1);
print("---------分割线--------");
}
if (GUI.Button(new Rect(400,100,100,100),"out修饰符" ))
{
print("---------分割线--------");
FucOut(out outNum);
FucOut1(out outNum1);
print("---------分割线--------");
}
}
}
3、工程下载
传送门:https://download.csdn.net/download/mr_sun88/10992663
参考:https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/params
4、推送
先空着
5、结语
本篇博客呢,日常学习记录,希望能对各位同学也有所帮助,另博主能力有限,若文中有出现什么错误的地方,欢迎各位评论指摘。
QQ交流群:806091680(Chinar)
该群为CSDN博主Chinar所创,推荐一下!我也在群里!
本文属于原创文章,转载请著名作者出处并置顶!!!!!