嗚嗚喔學習筆記

搜尋此網誌

2016年7月17日 星期日

C#-Delegate

Delegate

簡單的說就是能夠 "傳遞方法"

程式碼:
using System;
namespace SimpleEvent
{
    //建照標準函式 型態參數一樣才能註冊
    public delegate string DelegatePrint(string Name);

    class Suzanne
    {
        //必須和Delegate型態參數一模一樣
        public static string print01(string formDelegateStr )
        {
            Console.WriteLine("print 01 " + formDelegateStr);
            return "Done";
        }
        //必須和Delegate型態參數一模一樣
        public static string print02(string formDelegateStr)
        {
            Console.WriteLine("print 02 " + formDelegateStr );
            return "Done";
        }

        //必須和Delegate型態參數一模一樣
        public static string print03(string formDelegateStr)
        {
            Console.WriteLine("print 03 " + formDelegateStr);
            return "Done";
        }

        public static void Main()
        {
            //用print01來註冊 delegatePrint
            DelegatePrint delegatePrint = new DelegatePrint( print01 );

            //用print02來註冊 delegatePrint
            delegatePrint += print02;

            //這時候 delegatePrint => print01() , print02()
            Console.WriteLine(delegatePrint("FromDelegateStr"));

            //再增加一個 print03 //總共有 print01 , print02 , print03 三個函式了~
            delegatePrint += print03;

            //delegatePrint() => print01() , print02() , print03() 
            //因為剛剛註冊了三個函式
            Console.WriteLine(delegatePrint("FromDelegateStr"));

            Console.ReadLine();
        }
    }
}
結果: 

2016年7月15日 星期五

C#-Dictionary

查表
using System;
using System.Collections;
using System.Collections.Generic;

/// <summary>
///     A simple indexer example.
/// </summary>
class ConsolePrint
{

    static void Main(string[] args)
    {
        Dictionary<string, string> dctNewWay =
        new Dictionary<string, string>()
        {
            {"Key1", "AAAA"}, {"Key2", "BBBB"},
            {"Key3", "CCCC"}, {"Key4", "DDDD"}
        };

        dctNewWay.Add("Key5", "EEEE");

        String FindByKey = dctNewWay["Key1"]; 
        bool AnyKey = dctNewWay.ContainsKey("key1");
        Console.WriteLine("FindByKey = {0}", FindByKey);
        Console.ReadLine();
    }
}

2016年7月14日 星期四

C# - Property

程式碼:
using System;

/// <summary>
///     A simple indexer example.
/// </summary>
class ConsolePrint
{
    class User {
        private string FirstName = "王";
        private string LastName = "小名";
        public User() { }
        public string FullName
        {
            get
            {
                return this.FirstName + this.LastName;
            }
            set { }
        }
    }
    

    static void Main(string[] args)
    {
        var user = new User();
        Console.WriteLine("Username = {0}" , user.FullName );
        Console.ReadLine();
    }
}
輸出: Username = 王小明
結果: 可以當set get函式使用 , 方便封裝

C# - Ref - Class篇

計上一篇: http://tim12332013.blogspot.tw/2016/07/c-ref-out.html
來試試Class版本
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var test1 = new ClassTest();
            var test2 = new ClassTest();

            test1.value = 100;
            test2.value = 200;

            Console.WriteLine("Test1 value = {0} Test2 value = {1}", test1.value, test2.value);

            TestRefObject(test1, ref test2);

            Console.WriteLine("After TestRefObject function Test1 value = {0} Test2 value = {1}", test1.value, test2.value);

            Console.ReadLine();
        }

        private static void TestRefObject(ClassTest test1, ref ClassTest test2 )
        {
            test1.value = 101;
            test2.value = 202;

            test1 = new ClassTest();
            test1.value = 1001;

            test2 = new ClassTest();
            test2.value = 2002;
        }

        class ClassTest
        {
            public ClassTest() { }
            public int value = 0; 
        }
    }
}

輸出:
結論就是型別是Class 也一樣會有效果

2016年7月13日 星期三

C# - params

簡單說:可以使用多個參數
程式碼:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Fifth(new int[] { 1 , 2 , 3 , 4 , 5 });

            Fifth(66,55,44,33,22);
            Console.ReadLine();
        }
        
        private static void Fifth( params int[] fifth)
       {
            foreach (var v in fifth)
                Console.WriteLine("foreach fifth ->{0}", v);
       }
    }
}

C# - Ref Out

Ref , Out 都傳遞位置參考
Ref 一定要事前給值
Out 一定要 function 離開前給值

程式碼:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 100;

            Console.WriteLine("number = {0}",number);

            TestRef(ref number);

            Console.WriteLine("number = {0}", number);

            TestWithoutRef(number);

            Console.WriteLine("number = {0}", number);

            int outNumber ;

            TestOut(out outNumber);

            Console.WriteLine("outNumber = {0}" , outNumber);
            

            Console.ReadLine(); //Pause

        }
        
        private static void TestOut( out int nubmer )
        {
            nubmer = 1001;
        }

        private static void TestRef( ref int number )
        {
            number = number + 1;
        }

        private static void TestWithoutRef( int number )
        {
            number = number + 1;

            Console.WriteLine("In TestWithout number = {0}", number);
        }
    }
}

結果:

重結果可以推論出 都是傳參考值

2016年7月12日 星期二

好文連結分享

Design pattern - 工廠模式
https://rongli.gitbooks.io/design-pattern/content/chapter1.html

C#委託事件
http://blog.xuite.net/autosun/study/32614006-%5BC%23%5D+%E4%BA%8B%E4%BB%B6%E8%88%87%E5%A7%94%E6%B4%BE%EF%BC%88Delegate%EF%BC%89