C# 語法學習_Lesson 6

C# 語法學習_Lesson 6

欄位

「欄位」 是與類別或類別執行個體關聯的變數。
使用 static 修飾詞來宣告的欄位會定義靜態欄位。 靜態欄位只會識別一個儲存位置。 不論建立多少個類別執行個體,都只會有一個靜態欄位複本。
未使用 static 修飾詞來宣告的欄位會定義執行個體欄位。 每個類別執行個體都包含一個該類別所有執行個體欄位的個別複本。

在下列範例中,每個 Color 類別執行個體都具有 r、g 及 b 執行個體欄位的個別複本,但只有一個 Black、White、Red、Green 及 Blue 靜態欄位的複本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Color
{
public static readonly Color ;
Black = new Color(0, 0, 0);
public static readonly Color White = new Color(255, 255, 255);
public static readonly Color Red = new Color(255, 0, 0);
public static readonly Color Green = new Color(0, 255, 0);
public static readonly Color Blue = new Color(0, 0, 255);
private byte r, g, b;

public Color(byte r, byte g, byte b)
{
this.r = r;
this.g = g;
this.b = b;
}
}

如先前的範例所示,可以使用 readonly 修飾詞來宣告「唯讀欄位」 。 只有在欄位的宣告或在相同類別的建構函式中,才能對 readonly 欄位進行指派。

方法

方法 是實作物件或類別所能執行之計算或動作的成員。 存取「靜態方法」時,是透過類別來存取。 存取「執行個體方法」 時,是透過類別的執行個體來存取。

方法可能會有一份「參數」 清單和「傳回型別」 ,前者代表傳送給方法的值或變數參考,後者則指定方法所計算並傳回的值型別。 如果方法不會傳回值,則其傳回型別為 void。

1
2
3
4
5
6
7
8
9
10
11
public static string ddStr(string abc)
{
string resultStr = "";
...
return resultStr;
}

public void doSomething()
{
...
}

與型別相同,方法也可能有一組型別參數,而呼叫方法時,必須為這些參數指定型別引數。 與型別不同的是,型別引數通常可以從方法呼叫的引數推斷,而不需要明確指定。

在宣告方法的類別中,每一個方法的「簽章」 必須是唯一的。 方法的簽章是由方法的名稱、型別參數的數目以及其參數的數目、修飾詞和型別所組成。 方法的簽章並不包括傳回型別。

1
2
3
4
5
6
7
8
9
public void doSomething()
{
...
}

public string doSomething(string abc)
{
return abc;
}

參數

參數(Parameters)主要是用來傳遞值(value)或變數參考(variable references)給方法,方法中的參數會從叫用方法時(invoke)所指定的「引數」 取得其實際值。
參數有四種:
值參數(value parameters)
參考參數(reference parameters)
輸出參數(output parameters)
參數陣列(parameter arrays)

  • 值參數 適用於傳遞輸入的引數,此變數會從參數傳遞的引數取得其初始值並對應到區域變數,變更原本參數的值,並不會引響到已傳遞輸入引數的值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
class ValueExample
{
public string doSomething(string abc)
{
return abc;
}

public static void ValueTest()
{
string abc = "test123";
string result = doSomething(abc);
abc = "2345866";
Console.WriteLine($"{result} {abc}") //Ouputs "test123 2345866"
}
}
  • 參考參數 適用於以參考方式傳遞引數,參考參數傳遞的引數必須是含有定義值的變數,而且在方法執行的期間,參考參數和引數變數是使用相同的儲存位址。宣告參考參數時,是使用 ref 修飾詞來宣告
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
class RefExample
{
static void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = temp;
}
public static void SwapExample()
{
int i = 1, j = 2;
Swap(ref i, ref j);
Console.WriteLine($"{i} {j}"); // Outputs "2 1"
}
}
  • 輸出參數 適用於以參考方式傳遞引數,類似於參考參數,只不過它並不需要明確指派值給呼叫端提供的引數。宣告輸出參數時,是使用 out 修飾詞來宣告。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
class OutExample
{
static void Divide(int x, int y, out int result, out int remainder)
{
result = x / y;
remainder = x % y;
}
public static void OutUsage()
{
Divide(10, 3, out int res, out int rem);
Console.WriteLine("{0} {1}", res, rem); // Outputs "3 1"
}
}
  • 參數陣列 可允許將數目不固定的引數傳遞給方法。宣告參數陣列時,是使用 params 修飾詞來宣告。 只有方法的最後一個參數可以是參數陣列,而參數陣列的型別必須是單一維度陣列型別。
1
2
3
4
5
6
public class Console
{
public static void Write(string fmt, params object[] args) { }
public static void WriteLine(string fmt, params object[] args) { }
// ...
}

在使用參數陣列的方法內,參數陣列的行為與陣列型別的一般參數完全相同。 不過,在叫用含有參數陣列的方法時,可以傳遞單一的參數陣列型別引數,或是傳遞任意數目的參數陣列元素型別引數。 在後者的案例中,會自動建立陣列執行個體並以指定的引數將其初始化。 以下範例

1
2
3
4
5
6
7
8
9
10
//範例1
Console.WriteLine("x={0} y={1} z={2}", x, y, z);

//範例2
string s = "x={0} y={1} z={2}";
object[] args = new object[3];
args[0] = x;
args[1] = y;
args[2] = z;
Console.WriteLine(s, args);