iT邦幫忙

0

【C#學習筆記】01《快速入門筆記》

  • 分享至 

  • xImage
  •  

身為一位遊戲開發者,免不了需要多語言學習,像是業界常用的兩款引擎Unity及Unreal Engine,前者是用C#,後者則是使用C++進行開發(Blueprint邏輯也是C++)。
目前在工作上,我大部分是使用Unity進行開發,比較常寫C#,但先前的工作以及碩士期間,比較常用Unreal Engine進行開發整合,所以才會決定從C++開始學習。在兩種語言的交互下,很容易就會搞混或忘記語法,於是決定開始撰寫C#筆記,方便未來專案在轉換引擎時可以快速複習。

C#的部分會以隨手筆記為主,相對C++筆記會寫的比較隨興,還請見諒及指教。


【C#學習筆記】02《陣列與迴圈》


簡易輸出(output)

Console.WriteLine("Hello, World!");//自動換行
Console.Write("Hello");//不換行

//字串插值(推薦)
string name = "John";
int age = 20;
Console.WriteLine($"Name: {name}, Age: {age}");

//複合格式(舊寫法,類似c++的printf)
//只能用在Console.WriteLine
int x = 1;
Console.WriteLine("x = {0}", x);

算數運算子

int a = 1 - 2 + 3 * 4 / 2;//5
int b = 1 - (2 + 3) * 4 / 2;//-9

//遞增遞減運算子
int c = 10;
Console.WriteLine($"c = {c++}");//c = 10
Console.WriteLine($"c = {c}");//c = 11

int d = 10;
Console.WriteLine($"d = {++d}");//c = 11
Console.WriteLine($"d = {d}");//c = 11

//除法分母不能為0
int e = 10 / 0;//error

//縮寫
int f = 10;
f += 10;//f = f + 10

//"+"特殊用法
string a = "a" + "b";//ab
string b = "a" + 1;//ab
bool c = true;
string d = "a" + c + 1;//atrue1

布林值(bool)與運算

bool a1 = true;//true
var a2 = false;//false
var a3 = a1 && a2;//false
var a4 = a1 || a2;//true
var a5 = 3 > 1;//true
var a6 = 4 == 4;//true

//var,根據右邊賦值,自動推斷型別

條件判斷(if、else)

var a1 = true;
if (a1) Console.WriteLine("a1 is true");//if後面的程式碼只有一行,可以不用大括號
else Console.WriteLine("a1 is false");

if(!a1)//否定ai的值
{
    Console.WriteLine("a1 is false");
}

條件判斷範例

//閏年判斷
bool isLeapYear;
var year = 2024;

if (year % 100 == 0)//巢狀條件判斷
{
    if(year % 400 == 0) isLeapYear = true;
    else isLeapYear = false;
}
else
{
    if(year % 4 == 0) isLeapYear = true;
    else isLeapYear = false;
}
if(isLeapYear) Console.WriteLine($"{year} is leap year");
else Console.WriteLine($"{year}is not leap year");

三元條件運算子

基本格式:

condition ? value_if_true : value_if_false;

結構拆解:
condition:條件(回傳 true / false)
?:如果成立
::否則

//範例
int x = 10;

string result = (x > 0) ? "positive" : "negative";
Console.WriteLine(result);//positive
//閏年判斷
bool isLeapYear;
var year = 2024;

if(year % 100 == 0)
    isLeapYear = (year % 400 == 0) ? true : false; //三元運算子
else
    isLeapYear = (year % 4 == 0) ? true : false;
Console.WriteLine(isLeapYear ? $"{year} is leap year" : $"{year} is not leap year");

三元條件判斷適合用在
1.簡單條件
2.單一結果
3.賦值或輸出

條件判斷(switch)

int a = 2;
switch(a)//條件
{
    case 1:
        Console.WriteLine("a is 1");
        break;
    case 2:
    case 3:
        Console.WriteLine("a is 2 or 3");
        break;
    case < 10 or > 100:
        Console.WriteLine("a is less than 10 or greater than 100"); 
        break;
}

以上內容主要整理了C#在基礎輸入輸出、運算子以及條件判斷上的常用寫法。由於目前仍是在與C++交替使用的情境下學習,因此筆記內容會以「快速理解與對照」為優先,而不是完整語言教學。
實際開發時,可以發現C#在語法上相對簡潔,例如字串插值、型別推斷(var)以及三元運算子,都能有效提升開發效率;但同時也需要特別注意一些與C++不同的細節,例如型別安全、條件判斷結構,以及語法糖背後的實際行為。
未來會持續補充更多實務開發中常用的寫法,例如:
迴圈(for / foreach / while)
類別與物件(class / struct)
Unity常用API與設計模式
與C++/Unreal的對照整理

這份筆記的目標不是完整,而是在需要切換語言或引擎時,能快速喚回語感與基礎概念。如果有錯誤或更好的寫法,也歡迎交流與指正。


圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言