顯示具有 c sharp / c# 標籤的文章。 顯示所有文章
顯示具有 c sharp / c# 標籤的文章。 顯示所有文章

2016年6月29日 星期三

c# Regular Expression

Regular Expression

Regex regularExpression = new Regex("^[a-zA-Z0-9_]*$");


//contains only upper and lowercase letters, numbers, and underscores.




  • * ^ : start of string
  • [ : beginning of character group
  • a-z : any lowercase letter
  • A-Z : any uppercase letter
  • 0-9 : any digit
  • _ : underscore
  • ] : end of character group
  • * : zero or more of the given characters
  • $ : end of string
  • 2016年3月6日 星期日

    C# Coroutine

    Unity Coroutine

    使用的地方

    必須在MonoBehaviour或繼承MonoBehaviour的class中使用

    不可使用的地方

    yield不可在Update
    不可在FixedUpdate中使用

    啟動Coroutine

    StartCoroutine(string methodName)
    StartCoroutine(IEnumeratorroutine)

    停止Coroutine

    StopCoroutine(string methodName)
    StopAllCoroutines()

    2016年3月4日 星期五

    C# delegete


    C# delegete

    就像是把函式包裝過然後傳給別的函式使用。
    有點像是變數的泛型,因為不確定使用時的什麼類型,所以訂一個通用的類型來用。

    Unity Delegate

    //delegate 定義
    //以下是一個參數為string, 回傳值為int的函式
      delegate int Action(string amount);
    

    //宣告成一個物件,以便使用
      Action action;
    

    //接受傳入delegate物件
    private void UseAction(int money,Action action)
    {
      int n=action(money);
    }
    
    //假設有三個同參數同回傳值的函式
    //一開始不知道要用哪一個
    //就可使用delegate
    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);
    }
    

    可以把A的函式委託給B

    或是把B的函式委託給A



    多重呼叫delegate

    delegate一次註冊多個方法,呼叫時所有註冊的都會一起呼叫
    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;
        }
    }
    

    C# 泛型修飾詞

    C# in out 泛型修飾詞

    out 修飾詞

    MSDN
    out 關鍵字會指定型別參數為 Covariant
    可在泛型介面可使用
    可在委派中可使用
    // Covariant interface.
    interface ICovariant { }
    
    // Extending covariant interface.
    interface IExtCovariant : ICovariant { }
    
    // Implementing covariant interface.
    class Sample : ICovariant { }
    
    class Program
    {
        static void Test()
        {
           // ICovariant iobj = new Sample();
           // ICovariant istr = new Sample();
    
            // You can assign istr to iobj because
            // the ICovariant interface is covariant.
           // iobj = istr;
        }
    }
    

    in 修飾詞

    MSDN
    in 關鍵字會指定型別參數為 Covariant
    可在泛型介面可使用
    可在委派中可使用

    C# 方法參數修飾詞 out ref params

    C# in , out , ref , params


    params 修飾詞

    MSDN
    當傳入array時可用,此時參數的數目不定
    可傳入陣列
    可傳入元素
    可以什麼都不傳,此時陣列長度為0
    在params之後不許有其他參數(params要放在最後一個參數)
    且只能有一個params

    //當有這樣一個陣列
    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[]
    */
    

    ref 修飾詞

    MSDN
    把參數用傳參考的方式傳入函式
    在宣告和呼叫的參數前都加上ref
    參數在傳遞前一定要初始化
    被傳入的參數,在函式被修改,也會改到他原本的值
    屬性不能做為ref的參數

    這樣子不構成重載,會報錯
    public void SampleMethod(out int i) { }
    public void SampleMethod(ref int i) { }
    
    這樣子可以
    public void SampleMethod(int i) { }
    public void SampleMethod(ref int i) { }
    

    out 修飾詞

    MSDN
    把參數用傳參考的方式傳入函式
    參數在傳遞錢不一定要初始化
    在宣告和呼叫的參數前都加上out
    被傳入的參數,在函式被修改,也會改到他原本的值
    函式可宣告多個out參數
    在函式中一定要對out參數附值,否則報錯
    直接在函式中修改out參數,就等於是回傳值

    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;
        }
    }
    

    int 修飾詞

    傳入的參數不能被更動