在Unity中,C#变量声明方式为:
变量类型变量名;
例如:
string player = “tom”;
变量前面可以添加访问修饰符public protected private 。 其中 public的变量可以在Inspector视图中查看和编辑 ,不添加访问修饰符则默认为private(在JavaScript中默认为public)。
在C#中只能使用内建数组
C#里的函数格式为:
访问修饰符 返回值类型 函数名 (参数类型 参数1,参数类型 参数2,.......){
}
例如:
public int Sun(int num1,int mum2){
return num1 + num2;
}
C#中out 与 ref的用法:
1、out必须在函数体内初始化,在外面初始化没意义。也就是说,out型的参数在函数体内不能得到外面传进来的初始值。
2、ref必段在函数体外初始化。
3、两都在函数体的任何修改都将影响到外面。
例:
using System;
namespace ConsoleApplication1
{
class C
{
public static void reffun(ref string str)
{
str += " fun";
}
public static void outfun(out string str)
{
str = "test"; //必须在函数体内初始
str += " fun";
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
string test1 = "test";
string test2; //没有初始
C.reffun( ref test1 ); //正确
C.reffun( ref test2 ); //错误,没有赋值使用了test2
C.outfun( out test1 ); //正确,但值test传进去
C.outfun( out test2 ); //正确
Console.Read();
}
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。