網頁

2010年5月17日 星期一

C# Tutorial - 4. Statements and Expression Tutorial

本文主要介紹C#最基本要件和運算式,以供有興趣的朋友參考。


Statements
StatementC#最基本的組成,所有程式都是由Statement建構的,可以宣告區域變數、常數、呼叫方法、建立物件,或指派值給變數、屬性或欄位。
Statement通常以分號結尾。

常見的Statements
分類
C# 關鍵字
Declaration statements
xx=2
Expression statements
area = 3.14 * (radius * radius);
Selection statements
ifelseswitchcase
Iteration statements
doforforeachinwhile
Jump statements
breakcontinuedefaultgotoreturnyield
Exception handling statements
throwtry-catchtry-finallytry-catch-finally

Msdn Example:
    static void Main()
    {
        // Declaration statement.
        int counter;
 
        // Assignment statement.
        counter = 1;
 
        // Error! This is an expression, not an expression statement.
        // counter + 1; 
 
        // Declaration statements with initializers are functionally
        // equivalent to pointA declaration statement followed by assignment statement:         
        int[] radii = { 15, 32, 108, 74, 9 }; // Declare and initialize an array.
        const double pi = 3.14159; // Declare and initialize pointA constant.          
 
        // foreach statement block that contains multiple statements.
        foreach (int radius in radii)
        {
            // Declaration statement with initializer.
            double circumference = pi * (2 * radius);
 
            // Expression statement (method invocation). A single-line
            // statement can span multiple text lines because line breaks
            // are treated as white space, which is ignored by the compiler.
            System.Console.WriteLine("Radius of circle #{0} is {1}. Circumference = {2:N2}",
                                    counter, radius, circumference);
 
            // Expression statement (postfix increment).
            counter++;
 
        } // End of foreach statement block
    } // End of Main method body.
} // End of SimpleStatements class.

Expression
運算式是程式碼片段,可判定為單一的值、物件、方法或命名空間。
運算式可包含常值、方法引動過程、運算子和運算元,或「簡單名稱」(Simple Name)。簡單名稱可以是變數的名稱、型別成員、方法參數、命名空間或型別。

Msdn example:
((x < 10) && ( x > 5)) || ((x > 20) && (x < 25))
System.Convert.ToInt32("35")

閱讀全文...

C# Tutorial - 3. Operators, Types, and Variables Tutorial

本文主要介紹C#的Operators、Types和Variables,以供有興趣的朋友參考。


Variables
C# 是強型別語言,因此每一個變數和物件都必須有宣告的型別。

Types
C#內建的基礎型別有:
* Boolean Type - true and false
* numeric types
- Integrals
The Size and Range of C# Integral Types
Type
Size (in bits)
Range
sbyte
8
-128 to 127
byte
8
0 to 255
short
16
-32768 to 32767
ushort
16
0 to 65535
int
32
-2147483648 to 2147483647
uint
32
0 to 4294967295
long
64
-9223372036854775808 to 9223372036854775807
ulong
64
0 to 18446744073709551615
char
16
0 to 65535

- Floating Point
- Decimal
The Floating Point and Decimal Types with Size, precision, and Range
Type
Size (in bits)
precision
Range
float
32
7 digits
1.5 x 10-45 to 3.4 x 1038
double
64
15-16 digits
5.0 x 10-324 to 1.7 x 10308
decimal
128
28-29 decimal places
1.0 x 10-28 to 7.9 x 1028

*String - is a sequence of text characters.
字串物件是不變的,表示一旦建立就無法變更。修改字串會有新字串物件的建立,所以基於效能的考量,必須使用 StringBuilder  類別執行大量的串連或其他所需的字串管理,以後再做更詳細的介紹
C# Character Escape Sequences
Escape Sequence
Meaning
\'
Single Quote
\"
Double Quote
\\
Backslash
\0
Null, not the same as the C# null value
\a
Bell
\b
Backspace
\f
form Feed
\n
Newline
\r
Carriage Return
\t
Horizontal Tab
\v
Vertical Tab

Operators
C# 中,運算子是條件或符號,需要以一或多個運算式 (稱為運算元) 做為輸入並傳回值。
Operators with their precedence and Associativity
Category (by precedence)
Operator(s)
Associativity
Primary
x.y  f(x)  a[x]  x++  x--  new  typeof  default  checked  unchecked delegate
left
Unary
+  -  !  ~  ++x  --x  (T)x
left
Multiplicative
*  /  %
left
Additive
+  -
left
Shift
<<  >>
left
Relational
<  >  <=  >=  is as
left
Equality
==  !=
right
Logical AND
&
left
Logical XOR
^
left
Logical OR
|
left
Conditional AND
&&
left
Conditional OR
||
left
Null Coalescing
??
left
Ternary
?:
right
Assignment
=  *=  /=  %=  +=  -=  <<=  >>=  &=  ^=  |=  =>
right


閱讀全文...

2010年5月12日 星期三

C# Tutorial - 2. Command Line Arguments Tutorial

本文主要介紹C#如何處理從Command line帶進來的變數值,以供有興趣的朋友參考。



using System;
using System.Collections.Generic;
using System.Text;

namespace CmdLineInput
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("You entered the following {0} command line arguments:", args.Length);

            for (int i = 0; i < args.Length; i++) {
                Console.WriteLine("{0}", args[i]);
            }
        }
    }
}

Output:
You entered the following 4 command line arguments:
A
B
C
D

說明:
1. 不像 C 和 C++,程式名稱不會當做第一個命令列引數處理。
2. 使用 for 迴圈, 將帶入的參數逐一顯示在 console 上.

另一種方式, 使用 foreach statement

using System;
using System.Collections.Generic;
using System.Text;

namespace CmdLineArgus
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("You entered the following {0} command line arguments:", args.Length);

            foreach (string s in args)
            {
                Console.WriteLine("{0}", s);
            }
        }
    }
}

說明:
1. foreach 提供了一個簡單且清楚的方法來逐一查看陣列中的元素.


* 用 console mode 啟動測式應該都很OK, 但大部分都是殺雞用牛刀, 使用Visual Studio編輯器請照下面步驟:
Project -> 專案名稱的 Properties -> Debug -> Start Option -> Command line arguments
然後輸入程式啟始時想帶入的參數, 如 A B C D

閱讀全文...

C# Tutorial - 1. Hello World Tutorial

所有語言的第一個程式, Hello World
來點不一樣的, Hello C#, :)

using System;
using System.Collections.Generic;
using System.Text;

namespace Hello1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, C#");
        }
    }
}

說明:
1. Main 方法是程式的進入點,程式控制會在此開始和結束。
    Main method 必須在類別或結構中宣告。它必須為靜態且不應該為 public
    傳回型別可以是 void 或 int。
2. WriteLine method 屬於 System.Console class, 用來將資料顯示在 console 中.

程式碼其實可以更簡潔一點, 只是用Visual Studio產生專案時, 會加了一堆東西
閱讀全文...

2010年4月16日 星期五

Android疊疊樂


Android架構:
(1)Applications(應用程式)
(2)Application Framework(應用程式框架)
(3)Libraries(函式庫)
(4)Android Runtime(Android執行環境)
(5)Linux Kernel(Linux核心)
閱讀全文...

2010年4月15日 星期四

Android vs iphone 程式開發選擇

Android iphone 是近來最熱門的智慧型手機平台,如果要在這兩者間擇一來開發程式?網路上討論的很多,大致上有幾個評估點可以最為參考:


1.硬體

Iphone的硬體應用有些狹隘,有點不像智慧型手機,有點像遊戲機、變種的ipod(聽音樂看影片用)、行動上網裝置,更重要的一點,硬體規格完全由APPLE主導制定。

android硬體應用就很廣了,幾乎很多裝置都可以實裝,所以許多廠商都宣稱android硬體大勝iphone,至於這個對程式開發是不是福音,就有待評估了,越多不同的手機硬體,代表在整合開發上會遇到更多不同的狀況,解決這些情況所花的時間成本也是值得去考慮的。

2.軟體開發限制

iphone上開發軟體會有很多限制,有些類型的軟體,APPLE不允許開發,須遵守其相關規定,不然到時會影響到是否能在APP STORE上販賣。

Android就沒有什麼限制了,而且還是open source,身為程式設計師,光看到這個時,幾乎整個心都往android偏過去了。

3. 開發門檻

Iphonexcode+object C,有些人根本連聽過都沒聽過,還曾被評論過所有的程式語言就屬object c最難用,另一個門檻更是打死一堆人,要開發iphone的軟體,需要一台mac電腦,如果不在mac os上開發,光建立開發環境就夠你受的了。當你萬事俱備想開發iPhone版應用程式,並且在App Store上架販售者,必須先向蘋果註冊註冊費每年99美元,若需要技術支援服務,則得每年支付199美元。

AndroidEclipse+java,純OO的語言,可跨平台的開發,大量匯JAVA的程式人員,就會讓人覺得樂勝iphone了。開發android軟體上架販售,註冊費僅25美元,而且,只要付一次即可,我想,這更是吸引人的地方註冊費僅25美元,而且,只要付一次即可,我想,光這點就勝負很明顯了。

從事程式設計這麼多年,深深的覺得,門檻只是懶人的藉口而已,只有願不願意花時間和有沒有心去學習新的東西。Iphone門檻真的高嗎?App Store有套Doodle Kids軟體,一個新加坡九歲小孩寫出來的,他是為了三歲和五歲的妹妹而設計這套程式的,So, Iphone門檻真的高嗎?

4.軟體銷售拆帳比率

拆帳比都是37分帳,不同的是蘋果抽30%的利潤,而Android30%不是由Google取走,是給在地電信營運商,這種拆帳機制讓本土電信營運商更積極的支援、推廣Android

5.審核機制

iphone軟體要上架到app store,會經過apple層層審核,上架後還有可能被無預警下架;android則是門戶大開,完全沒有審核機制,所以上面什麼程式都有,涉及色情、犯罪和暴力也在其中。有兩個小孩的我,還是覺得APPLE的機制比較好一點。

Androidiphone各有其優劣,各有其擁護的粉絲,而我自己的感覺,android適合宅男工程師,這群人總想自己在上面開發屬於自己的軟體,也適合一些商務人士,因為操作流暢度沒iphone好,剩下就只能說是商務功能了,雖然說大部分都是google平台服務,如GMAILSearch等。

相對地,iphone深深吸引了一堆年輕人和高消費族群,而且這群人卻是消費力最猛的一群,整天iphone不離手,感覺好像中毒一樣,至於iphone的熱度會持續多久,是否能持續發熱,都是未知數。


閱讀全文...