最近已经开始做项目了,感觉自己是全组里技术最弱的。之前真的没有做过三消核心部分的代码,
Unity又刚开始用,很多用法都不知道,感觉有些吃力。平时上班还好,能督促自己积极的思考和学习。
可是一旦放假休息,就感觉自己很颓废。晚上睡不着,白天醒不了,一天基本一顿饭。
所以一个人,真的很害怕放假。
反正我写的Blog也不会有人来看,只是记录一些自己的东西。
开始说正题:
一、ref 的用法——引用
C#提供一种强制按引用传递参数的方法。这种方法既能用在值类型上,也能用在引用类型上。
就是同时在方法的声明和调用上使用 ref 关键字。
using System;
class Program
{
static void Main()
{
int i = 10;
fun(ref i);
Console.WriteLine("i = " + i);
Console.ReadLine();
}
static void fun(ref int i)
{
i = 100;
}
}
输出结果为 : i = 100
二、out 的用法——输出参数
在C++中,获得方法的结果要么是通过返回值,要么是通过由引用或指针传递的参数。
而在C#中,除了返回值,引用,还提供了一种比较正规的方法。就是使用 out 关键字来标记需要返回的参数。
<span style="font-size:18px;">using System;
class MyClass
{
public string TestOut(out string i)
{
i = "使用out关键字";
return "out参数";
}
}
class test
{
static void Main()
{
string x;
MyClass app = new MyClass();
Console.WriteLine(app.TestOut(out x));
Console.WriteLine(x);
Console.ReadLine();
}
}</span>
输出结果为:
三、params 的用法——不定数目的参数
C++中允许函数带有默认参数,允许不定数目的参数。但C#中不允许函数参数带有默认值。默认值这种功能,只能用函数重载来代替实现了。
但是C#允许方法带有不定数量的参数。使用params关键字,且参数必须是参数表中的最后一个参数。
using System;
class Program
{
static void Main()
{
fun("Call 1");
Console.WriteLine("\n");
fun("Call 2", 2);
Console.WriteLine("\n");
fun("Call 3", 3.2, "hey look at here", false);
Console.ReadLine();
}
static void fun(string str, params object[] args)
{
Console.WriteLine(str);
foreach (object ob in args)
{
if (ob is int)
{
Console.Write(" int : " + ob);
}
else if (ob is double)
{
Console.Write(" double : " + ob);
}
else if (ob is string)
{
Console.Write(" string : " + ob);
}
else
{
Console.Write(" other type : " + ob);
}
}
}
}
输出结果为: