想搵音符嘅話,請睇C♯ (音符)

C#英文C Sharp)係微軟.NET Framework 標準設計出嚟嘅一隻高階程式語言,喺 2000 年發行。C# 由 C 程式語言C++ 衍生出嚟,同 Java 程式語言亦有唔少相似之處。C# 用途廣泛,支援多種編程範式,尤其係物件導向編程,而且仲同多種遊戲引擎相容,喺廿一世紀初係編寫遊戲程式嘅一種常用語言[5]

Quick Facts 編程範式, 家族 ...
C#
Thumb
編程範式結構化物件導向泛型
家族C
設計者微軟
編程員微軟
第一次出現2000年 24年之前 (2000)
穩定版本
10[1] / 2021年11月8號 2年之前 (2021-11-08)
操作系統WindowsLinuxMac OS XAndroid
軟件授權
  • Roslyn compiler: MIT License[2]
  • .NET Core: MIT License[3]
副檔名.cs, .csx
網站C# Language
Major implementations
.NET Core.NET FrameworkMonoDotGNU
Dialects
Spec#英文Spec SharpPolyphonic C#英文Polyphonic C sharp
啟發語言
C++JavaEiffel英文EiffelModula-3Object Pascal
影響語言
Clojure[4] D語言F#英文F SharpJava 5、NemerleVala英文Vala
Close

概論

一個用 C♯ 寫嘅 Hello World 程式如下[6]

using System; // 會用到 System 入面嗰柞指令;

class Hello // class 可以包括 method(子程序)
{
    static void Main() // Main 表示主程序,void 表示個程序唔會俾乜嘢 output;
    {
        Console.WriteLine("Hello, World"); // 喺 Console 出「Hello, World」嘅字樣;
    }
}

基本結構

一個 C# 程式基本結構如下[7]

using System;
namespace YourNamespace
{
    class YourClass
    {
    }

    struct YourStruct
    {
    }

    interface IYourInterface
    {
    }

    delegate int YourDelegate();

    enum YourEnum
    {
    }

    namespace YourNestedNamespace
    {
        struct YourStruct
        {
        }
    }

    class YourMainClass
    {
        static void Main(string[] args)
        {
            // 主程式喺呢度...
        }
    }
}

一個 C# 程式另一個可能嘅結構如下,呢種結構常用於遊戲引擎嘅 C# 手稿(簡單講用嚟黐落去啲遊戲物件嘅程式):

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    // 初始化
    void Start()
    {
        // 個程式喺一開始嗰陣會行一次 Start 嘅碼。
    }

    void Update()
    {
        // 個程式喺開始後每個時間會行一次 Update 嘅碼。
    }
}

資料類型同變數

C# 支援好多種資料類型。C# 程式入面啲變數可以屬多種嘅資料類型,包括咗:

... 等等。

句法

控制流程

C#容許多種控制流程功能,包括:

... 等等。

其他

  • ToString()
  • System.Console.WriteLine("")
  • System.Console.ReadLine("")

睇埋

參考資料

Wikiwand in your browser!

Seamless Wikipedia browsing. On steroids.

Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.

Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.