Date: 2017/05/10
6.為數組的每一項賦相同初值
int[] array = Enumerable.Repeat(2, 4).ToArray();
// 數組長度為4, 每一項的值為2
5.委托的簡單簡單寫法
using UnityEngine;
public class ShortDelegate {
delegate int IntegerPipe (int input);
IntegerPipe AddOne = input => ++input;
Func<int, int> ReduceOne = input => --input;
delegate void NoReturnNoInput ();
NoReturnNoInput PrintInt = () => { Debug.Log(1); };
Action PrintString = () => { Debug.Log("1"); };
delegate bool ReturnBool (int input);
ReturnBool GetInegerValue = input => input != 0;
Predicate<string> GetStringValue = input => input != "" && input != null;
/*
a. Delegate至少0個參數土浸,至多32個參數,可以無返回值员舵,也可以指定返回值類型
b. Func至少0個參數扯躺,至多4個參數捉兴,根據返回值泛型返回。必須有返回值录语,不可void
c. Action至少1個參數倍啥,至多4個參數,無返回值
d. Predicate至少1個參數澎埠,至多1個參數虽缕,返回值固定為bool
*/
}
4.lamada 表達式代替匿名函數
// 無參數,無返回值
() => { int a = 1; }
// 無參數蒲稳,有返回值
() => {
int a = 1;
return a;
}
() => int a = 1;
// 有參數彼宠,有返回值
(a, b) => { return a + b; }
(a, b) => a + b
// 單參數可省略括號
a => a + 1
/*********/
a => b => a + b
3.約束類型
T AddChildComponent<T> (string findPath) where T: MonoBehaviour {
var childTransform = transform.FindChild(findPath);
return childTransform.gameObject.AddComponent<T>();
}
2.常量
public class Foo {
// 靜態(tài)常量: 聲明時必須賦值, 只能編譯時初始化
const int MAX_COUNT = 6;
// 實例常量: 可以在運行時初始化
readonly Vector3 POSITION = new Vector3(1, 12, 3);
readonly Color COLOR;
public Foo (Color color) { COLOR = color; }
}
1.獲取泛型默認值
public T getDefault<T> () { return default(T); }