2016年9月19日 星期一

[C#語言入門] New 的使用方法

Just a note to myself ...


寫法一:

          new Form() { Text = "Hello" }.ShowDialog();


寫法二:

           new Form().ShowDialog();


寫法三:

            //new : 可以調用實例(instance) 的初始化器, 還可以初始化多個屬性  ============
            Form myForm = new Form() { Text = "Hello", FormBorderStyle = FormBorderStyle.SizableToolWindow };  
            myForm.ShowDialog();


寫法四:

            //new : 可以調用實例(instance) 的初始化器  ==============
            Form myForm = new Form() { Text = "Hello" };  //設定屬性的初始值
            myForm.ShowDialog();


寫法五:

            //new : 可以調用實例(instance) 的構造器 =======================
            Form myForm = new Form();
            myForm.Text = "Hello";
            myForm.ShowDialog();


2016年9月18日 星期日

[C#] 使用 String.PadLeft 方法設定長度,並填補字串

Just a note to myself..


String.PadLeft (Int32, Char)
string xStr = Convert.ToString(x, 2).PadLeft(32, '0');




For test...

            #region 使用"~" 按位取反

            int x = 12345678;
            int y = ~x;
            string xStr = Convert.ToString(x, 2).PadLeft(32, '0');
            string yStr = Convert.ToString(y, 2).PadLeft(32, '0');
            Console.WriteLine(xStr);
            Console.WriteLine(yStr);

            #endregion

2016年9月16日 星期五

[C#語言入門] 使用迴圈(For Loop) 遞迴(Recursive)、數學公式,從1加到100

練基礎,筆記一下...
以下"方法三: 使用數學公式"是最好的方法。


方法一: 使用迴圈

   //使用For迴圈
        public int sumfrom1tox(int x)
        {
            int result = 0;
            for (int i = 1; i < x + 1; i++)
            {
                result = result + i;
            }
            return result;
        }

方法二: 使用遞迴

       //使用遞迴: 暫存用得多
        public int sumfrom1tox(int x)
        {
            if (x == 1)
            {
                return 1;
            }
            else
            {
                int result = x + sumfrom1tox(x - 1);
                return result;
            }
        }


方法三: 使用數學算式

        //數學算式
        public int SumFrom1ToX(int x)
        {
            return (1 + x) * x / 2;
        }



[C#語言入門] 以事件(Event)為側重的類別(Class) -以Timer為例

 Just a note to myself...


using System.Windows.Threading;

namespace EventSample
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DispatcherTimer time = new DispatcherTimer();  //實例化一個 Timer
            time.Interval = TimeSpan.FromSeconds(1);       //設定間隔多少時間來引發事件
            time.Tick += time_Tick; //掛接事件處理器,當這個件事觸發的時候, time_Tick() 這個方法就會執行
            time.Start();  //時鐘開始
        }

        void time_Tick(object sender, EventArgs e)
        {
            txtTime.Text = DateTime.Now.ToString();
        }
    }
}


2016年9月15日 星期四

[C#語言入門] 使用 new 創建類別(class)的實例化(instance)

Just a note to myself…
Class & Instance 基礎觀念....



        /// <summary>
        /// 範例2: 2個引用變數,使用同一個實例(instance),同一個對象。像 ref
        /// </summary>
        private static void test2()
        {
            Form myForm1;
            Form myForm2;
            myForm1 = new Form();
            myForm2 = myForm1;
            myForm1.Text = "It is test";
            myForm2.Text = "I changed it";
            myForm1.ShowDialog();
        }




2016年9月8日 星期四

[C#] Ref / Call by Reference

遇到就筆記一下。MSDN


Call by reference

虛擬參數(formal parameter)與實際參數(actual parameter)位於相同的記憶體位址。
因此function內對於ref value 變數值的變更, 會直接影響到外面的value變數。

使用方法: 加上 ref。



簡單來說…

1. ref 是進行 call by reference 的行為,即是改變 function 引數的值,外面的參數也會跟著改變。
2. function 的 ref 的參數,在傳入之前必須要先初始化。


e.g. 使用ref 的參數,必須在傳遞參數前先初始化變數

int num = 99;  
Test(ref num);

public void Test(ref int num)
{
      //TODO something ...
}


Reference:
1.https://gdfan1114.wordpress.com/2005/06/13/c-學習筆記02-繼承-參數傳遞/
2.http://blog.xuite.net/merci0212/wretch/141175640-%5B轉載%5DC%23+-+函數call+by+value%2Freference
3.http://unicorntudo.blogspot.tw/2013/07/c7-call-by-valuereference.html
4.http://unicomleaner.blogspot.tw/2013/04/crefout.html

2016年9月7日 星期三

[C#] Switch Case 在不同的Case區域,宣告相同的變數

筆記一下,今天遇到的情況...


狀況描述:在Switch Case 不同的區塊,宣告相同的變數,會出現錯誤。

錯誤範例:
switch (item)
{
    case "1":
        int temp = 1;
        break;
        
    case "2":
        int temp = 2;
        break;
}

錯誤訊息:己經在此範圍定義名為”temp”的區域變數。

解決方法:在 Case 區段加入 { } 括號。

正確範例:
switch (item)
{
    case "1":
        {
            int temp = 1;
            break;
        }
    case "2":
        {
            int temp = 2;
            break;
        }
}


Reference: 
http://stackoverflow.com/questions/11409709/c-sharp-switch-case-share-the-same-scope
http://goodmanroc.blogspot.tw/2012/08/c-swith-case.html