C# Basic

#C#

Posted by StuartHsu on 2022-03-06

Introduction to C# and .NET Framework

C# vs .NET

  • C# is a programming language
  • .NET is a framework for building appliction on Windows
    • .NET framework 的使用並不受限於 C#
    • 例如 F#、Visual Basic .NET 都可使用 .NET framework

.NET

包含:

  1. CLR (Common Language Runtime)
  2. Class Library - that we use for building applications

CLR

  • C/C++ 編譯(compiler)後成 native code(machine code)讓作業系統(Windows x86)讀懂並能夠運行,但此 native code 搬到不同作業系統如 Linux 就無法運行。
  • Java 編譯完後是並不是直接轉成 machine code,而是轉成 ByteCode。
  • C# 參考 Java 的做法,當 compile 應用程式時,C# 編譯器將 code 編譯成 IL code(Intermediate Language),如此一來就不會依賴於電腦的作業系統。
  • 而當運行 C# application 時,CLR 便將 IL code 編譯成 Native machine code 讓電腦運行,此過程及稱為 JIT(Just-in-time Complilation)。

Architecture of .NET Application

  • 由一堆 Class 組成
  • Class 包含了 Data(attributes) 與 Methods(functions)
    • Methods 負責執行 code,並包含程式邏輯
    • Data 表示、呈現應用程式的 state

Namespace

應用程式中有一堆 class,需要有方法去管理這些 class,因此透過 namespace 來將相關的 class 分類(group)在一起。

A container for classe

Assembly(DLL or EXE)

  • 而一堆 namespace 則透過 assembly 來將相關的 namespace 分類(group)在一起。
  • 一個 assembly 代表一個檔案(DLL or EXE),其中包含一個或多個 namespace 與 class。
  • EXE file:表示程式可以被執行
  • DLL file:是包含可以跨不同程序重複使用的程式的文件。
    • A single unit of deployment of .NET application.
    • It's a file, in the form of a executable(exe) or a DLL, that contains one ore more namespaces and classes.

Application

最後,應用程式將所有的 assembly 一起 compiler 來運行。


Primitive Types and Expressions

Varibales & Constants

  • Varible:a name given to a storage location in memory
  • Constant:an immutable value,在 compile 階段就已經決定值,在整個應用程式執行階段都不可改變

Primitive types:

Overflowing

C# 並不會檢查 overflow

byte number = 255;
number = number + 1; // 0

可透過 checked 來檢查是否 overflowing,並拋出 exception

checked
{
    byte number = 255;
    number = number + 1; // exception
}

Scope

Where a variable / constant has meaning

{
    byte a = 1;
    {
        byte b = 2;
        {
            byte c = 3;
            // can access a,b,c
        }
        // can access a,b
    }
    // can access a
}

Type Conversion

Implicit Type Conversion

隱含類型轉換

// runtime 會轉換
byte b = 1; // 00000001
int i = b;  // 00000000 00000000 00000000 00000001

int i = 1;
byte b = i; // compile 不會過,因為會 data lost

Explicit Type Conversion

明確類型轉換

int i = 1;
byte b = i; // 不會 compile,因為會 data lost

// 因此需要 Explicit Type Conversion 告訴它就是要轉型,不管會不會 data lost
byte b = (byte)i;

Non-compatibale types

string s = "1";
int i = (int)s; // 不會 compile,因為 string 和 int not compatitable

string s = "1";
// 透過以下方法轉型
int i = Convert.ToInt32(s);
int j = int.Parse(s);

Operators

Logic Operator

邏輯運算子
例如:&&、||

Bitwise Operator

位元運算子,將要比較的內容先轉換成二進位以做比較
例如:&、|

Non-Primitive Types

Classes

Combines related variables (fields) and functions (methods)

public class Person
{
    public string Name; // field

    public void Introduce() // method
    {
        Console.WriteLine("Hi");
    }
}

var person = new Person(); // CLR 會自動處理 object memory 的分配以及回收
person.Name = "Test";
person.Introduce();

Static Modifier

  • 不需要先 new object,可以直接取用 class 內的 static 方法
  • 且無法透過建立 object 來取用 static 成員
public class Calculator
{
    public static int Add(int a, int b)
    {
        return a + b;
    }
}

int result = Calculator.Add(1,2);

Why static?
透過 static 建立的方法,可以集中在 class 內,佔用的記憶體位置也只需要一份,若不是 static,則每次建立 object 都會將方法重新建立一個,並額外佔用記憶體

  • Non-static method
    Non-static method
  • Static method
    Static method

    常用的 Console.WriteLine 也是一個 static method

Structs

基本上 99% 不會用 struct,通常用來建立些小的物件才會用...

public struct RgbColor
{
    public int Red;
    public int Green;
    public int Blue;
}

Arrays


#C#







Related Posts

MTR04_0624

MTR04_0624

Redux, useSelector, useDispatch

Redux, useSelector, useDispatch

[ React 筆記 ] input使用useRef存取子元件

[ React 筆記 ] input使用useRef存取子元件


Comments