字串的操作一直是各語言的重頭戲,本文主要介紹C#常用操作字串的方法,以供有興趣的朋友參考。
C# 使用 string 來宣告字元陣列。其常值使用雙引號來高告。
mark528 發表在 痞客邦 留言(0) 人氣(2,122)
陣列是各語言最常用的資料結構,本文主要介紹C#操作陣列的方法,以供有興趣的朋友參考。
陣列是一種資料結構,其中會包含多個相同型別的變數。陣列是用型別宣告:
type[] arrayName;
mark528 發表在 痞客邦 留言(0) 人氣(1,484)
本文主要介紹C#最基本要件和運算式,以供有興趣的朋友參考。
Statements
Statement為C#最基本的組成,所有程式都是由Statement建構的,可以宣告區域變數、常數、呼叫方法、建立物件,或指派值給變數、屬性或欄位。
mark528 發表在 痞客邦 留言(0) 人氣(160)
本文主要介紹C#的Operators、Types和Variables,以供有興趣的朋友參考。
Variables
C# 是強型別語言,因此每一個變數和物件都必須有宣告的型別。
mark528 發表在 痞客邦 留言(0) 人氣(114)
本文主要介紹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
mark528 發表在 痞客邦 留言(0) 人氣(1,716)
所有語言的第一個程式, 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產生專案時, 會加了一堆東西
mark528 發表在 痞客邦 留言(0) 人氣(228)