Regular Expression
Regex regularExpression = new Regex("^[a-zA-Z0-9_]*$");//contains only upper and lowercase letters, numbers, and underscores.
delegate int Action(string amount);
Action action;
private void UseAction(int money,Action action) { int n=action(money); }//假設有三個同參數同回傳值的函式
private int TestA(int num) {} private int TestB(int num) {} private int TestC(int num) {} private void UseA() { //TestA指定給action,傳進UseAction //UseAction接受為派類型為參數 //TestA必須是int為參數,回傳值為int的函式 action=TestA; //action+=TestA; 也可以這樣用 UseAction(3,action); } private void UseB() { //TestA指定給action,傳進UseAction action=TestB; //action+=TestB; 也可以這樣用 UseAction(3,action); } private void UseC() { //TestA指定給action,傳進UseAction action=TestC; //action+=TestC; 也可以這樣用 UseAction(3,action); }
using UnityEngine; using System.Collections; public class MulticastScript : MonoBehaviour { delegate void MultiDelegate(); MultiDelegate myMultiDelegate; void Start () { myMultiDelegate += PowerUp; myMultiDelegate += TurnRed; if(myMultiDelegate != null) { myMultiDelegate(); } } void PowerUp() { print ("Orb is powering up!"); } void TurnRed() { renderer.material.color = Color.red; } }
// Covariant interface. interface ICovariant{ } // Extending covariant interface. interface IExtCovariant : ICovariant { } // Implementing covariant interface. class Sample : ICovariant { } class Program { static void Test() { // ICovariant
Object[] ary = new object[3] { 300, 'x', "abc" };//這個方法的參數宣告為params
private void TestFunc(params Object[] ary)//我們可以直接傳入一組物件
TestFunc(200,'d',"xxx");
public class MyClass { public static void UseParams(params int[] list) { for (int i = 0; i < list.Length; i++) { Console.Write(list[i] + " "); } Console.WriteLine(); } public static void UseParams2(params object[] list) { for (int i = 0; i < list.Length; i++) { Console.Write(list[i] + " "); } Console.WriteLine(); } static void Main() { // You can send a comma-separated list of arguments of the // specified type. UseParams(1, 2, 3, 4); UseParams2(1, 'a', "test"); // A params parameter accepts zero or more arguments. // The following calling statement displays only a blank line. UseParams2(); // An array argument can be passed, as long as the array // type matches the parameter type of the method being called. int[] myIntArray = { 5, 6, 7, 8, 9 }; UseParams(myIntArray); object[] myObjArray = { 2, 'b', "test", "again" }; UseParams2(myObjArray); // The following call causes a compiler error because the object // array cannot be converted into an integer array. //UseParams(myObjArray); // The following call does not cause an error, but the entire // integer array becomes the first element of the params array. UseParams2(myIntArray); } } /* Output: 1 2 3 4 1 a test 5 6 7 8 9 2 b test again System.Int32[] */
public void SampleMethod(out int i) { } public void SampleMethod(ref int i) { }這樣子可以
public void SampleMethod(int i) { } public void SampleMethod(ref int i) { }
private void Test(out int a, out int b) { a =3; b=5; } private void Call() { int x=0; int y=4; } Test(out x, out y);
class OutReturnExample { static void Method(out int i, out string s1, out string s2) { i = 44; s1 = "I've been returned"; s2 = null; } static void Main() { int value; string str1, str2; Method(out value, out str1, out str2); // value is now 44 // str1 is now "I've been returned" // str2 is (still) null; } }