iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 11
0
自我挑戰組

.net 後端起手式 30天認識 C# 系列 第 11

DAY 11 類別(二)

類別

接續昨天的話題,今天來聊聊靜態成員、擴充功能與命名空間。

靜態成員

使用 static 修飾詞來宣告靜態成員,而靜態成員屬於類型本身,而不是特定物件。

也就是類別成員如果宣告成靜態的成員,那麼在 new 成物件時不能去使用,因為該成員不屬於這個物件,而是屬於類別。

像 DAY 08 所舉例的方法,以 string 這個類別來舉例

由類別本身來存取

string str = string.Format ("這是{0}。", "靜態方法");
Console.WriteLine ("這也是靜態方法");

由物件本身來存取

string str = "測試用物件本身來存取";
string str2 = str.Replace ("測試","非靜態方法");

所以如果宣告成靜態成員,只要擁有該類別的存取權就能直接使用類別的成員,優點是可以不需要 new 出一個物件,就能使用該類別的成員,缺點是這些靜態成員因為是類別本身,所以會失去物件之間的差異性。

擴充方法

知道如何使用靜態成員後,就可以來撰寫擴充方法了,

擴充方法可讓您在現有類型中「加入」方法,而不需要建立新的衍生類型、重新編譯,或是修改原始類型。

簡單說就是擴充現有類別內的方法,例如你想在 int 裡面多一個方法,這個方法只要一執行就會回傳本來的值乘二。

class Program {
  static void Main (string[] args) {
    int i = 1;
    int i2 = i.Add2 ();
    Console.WriteLine ($"i: {i}, i2: {i2}");
    Console.ReadKey ();
  }
}

static class MyExtensions {
  public static int Add2 (this int number) {
    return number + 2;
  }
}

我們建立一個靜態類別,並將靜態方法第一個參數用 this 關鍵字,就可以做出一個擴充方法,除此之外與一般的方法一樣。

class Program {
  static void Main (string[] args) {
    int i = 1;
    int i2 = i.Add2n (6);
    Console.WriteLine ($"i: {i}, i2: {i2}");
    Console.ReadKey ();
  }
}

static class MyExtensions {
  public static int Add2n (this int number, int n) {
    return number + 2 * n;
  }
}

衍生類別會在後面介紹。

命名空間

可以看到一開始建立的專案,程式碼文件 Program.cs 剩下 namespace 與 using 這兩個關鍵字還沒解說,這邊一次講完。

先附上我目前的程式碼文件並解說這兩個關鍵字

using System;

namespace demo {
  class Program {
    static void Main (string[] args) {
      int i = 1;
      int i2 = i.Add2n (6);
      Console.WriteLine ($"i: {i}, i2: {i2}");
      Console.ReadKey ();
    }
  }

  static class MyExtensions {
    public static int Add2n (this int number, int n) {
      return number + 2 * n;
    }
  }
}

namespace

命名空間。

因為程式在執行時不會去看說你的檔名是什麼,而是去找命名空間是什麼,像是我們這份文件主要的內容都在命名空間為 demo 裡面,外部文件如果要參考並存取這份文件,就可以用 demo.xxxx 來讀取內容。

using

建立命名空間的別名(Alias),或用來匯入其他命名空間中定義的類別。

就像上述範例的 using System;,而 System 就是系統預設的一個命名空間,透過 using 我們就可以直接使用他的類別。如果不使用 using 也可以使用該類別,因為專案建立時,已經參考很多很多你不知道的東西,using只是把該命名空間整個匯入而已。

namespace demo {
  class Program {
    static void Main (string[] args) {
      int i = 1;
      int i2 = i.Add2n (6);
      System.Console.WriteLine ($"i: {i}, i2: {i2}");
      System.Console.ReadKey ();
    }
  }

  static class MyExtensions {
    public static int Add2n (this int number, int n) {
      return number + 2 * n;
    }
  }
}

若不同的命名空間內有一樣名稱的類別,則需要標註命名空間,但命名空間可能很長很長,所以用 using 指示時可以建立別名如

using S = System;

namespace demo {
  class Program {
    static void Main (string[] args) {
      int i = 1;
      int i2 = i.Add2n (6);
      S.Console.WriteLine ($"i: {i}, i2: {i2}");
      S.Console.ReadKey ();
    }
  }

  static class MyExtensions {
    public static int Add2n (this int number, int n) {
      return number + 2 * n;
    }
  }
}

題外話:這解釋是對 using 的指示詞用法,如果想知道另外一種用法,可以到文末參考連結觀看保哥的文章,有很詳細的說明。

一些小結論

今天介紹靜態成員了解靜態成員的使用方式與時機,擴充方法可以讓我們將現有的類別擴充想要的方法,命名空間則可能要接觸到專案後才可能會使用,教學文的範例程式不會跨命名空間撰寫,簡單的內容分開寫沒有太大的意義。

感謝閱讀。

參考連結
static (C# 參考) | Microsoft Docs
擴充方法 (C# 程式設計手冊) | Microsoft Docs
The Will Will Web | 關於 C# 的 using 陳述式在實務應用上的基本觀念


上一篇
DAY 10 類別
下一篇
DAY 12 繼承
系列文
.net 後端起手式 30天認識 C# 30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言