Unity中的C#规则

命名

文件名和Class要一致(CamelCase)

类公共和保护类型Property(CamelCase)

类的公共和保护类型Fields(CamelCase)* 先采用.Net的命名方法,如果出现问题以后改成小写。

Methord和Function(CamelCase)

私有字段或者Property(_camelCase)

parameter(camelCase)

定义变量的位置最好在使用前,离得越近越好

 

Type Var

    var x = 0; //x is an int
    var x = 0f; //x is a float
    var x = new Dictionary<string, float>(); // x is a dictionary of string to float
View Code
这样有的时候会使代码更容易阅读
var lookupWeight = new Dictionary<string, float>(); 
Dictionary<string, float> lookupWeight = new Dictionary<string, float>();
//第一个更好读
View Code

同时也不用关心返回的到底是什么类型

var newEnemy = CreateEnemy("Bob", EnemyType.pedanticProgrammer);
newEnemy.EvolveNow();  //Intellisense knew what newEnemy was
                          //I don't need to
View Code

This becomes even more important when using Linq or anonymous classes because it can actually be very hard to work out what the return value is, even though you (and again, intellisense) know exactly how to use it.

 SubClass and Enums

SubClass可以定义在一个类外或者内,如果只是为了作为另外一个类或者类的方的reference那么定义在类内。 If you define subclasses then they should come at the bottom of your outer class definition.  Remember to use #regions to simplify your file structure.

 Enum的话如果经常在类外使用,最好定义在gloable的位置。

 

 

posted @ 2013-06-27 16:36  若愚Shawn  阅读(404)  评论(0编辑  收藏  举报