const
vs readonly
C#
Programming
const
readonly
const
比較,readonly
修飾完依然是一般成員(member variable)使用VS2019
新增一個ConsoleApp(圖1)
Learn More →
對該專案的Solution新增一個Library,中文會叫做「程式庫」(圖2)
Learn More →
Learn More →
新增對程式庫的相依性(新增Reference) (圖3)
Learn More →
Lib
namespace ExampleLib
{
public class Constant
{
public static readonly string ReadOnlyString = "Readonly 1";
public const string ConstString = "Const 1";
}
}
ConsoleApp
using System;
using ExampleLib;
namespace Example
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"{Constant.ReadOnlyString}");
Console.WriteLine($"{Constant.ConstString}");
Console.ReadKey();
}
}
}
建置輸出後,沒意外會出現
Readonly 1
Const 1
把ConsoleApp的檔案總管打開後,會看到 (圖4):
Learn More →
兩個主要的檔案,一個是執行檔(.exe),另一個程式庫(.dll)
再來更新我們程式庫(DLL)
namespace ExampleLib
{
public class Constant
{
public static readonly string ReadOnlyString = "Readonly 2";
public const string ConstString = "Const 2";
}
}
記得只要更新程式庫,不要直接按
F5
全部都建置了
然後回到ConsoleApp的資料夾,把程式庫建置出來的元件全部複製一份,複製到ConsoleApp的資料夾裡。
之後再次打開執行檔,會發現readonly
跟著程式庫更新了,但const
沒有
Readonly 2
Const 1
這就是上面所說的編譯時期常數和執行時期常數
持續更新中!有用到就會更新上來。
Mar 19, 2024以下記錄先前用過的插件與開源專案 DOTween UI特效 - mob-sakai/UIEffect Debugging - yasirkula/UnityIngameDebugConsole
Jan 19, 2022:::success :sunglasses: 持續更新中....因為坑只會越踩越多 ::: 按鈕點下去沒反應... :::info UI Button不能按可能來自以下這幾個原因 ==請先利用EventSystem查看點到甚麼物件== Button沒有Raycast Target(預設為Image元件),或是Raycast Target沒有打勾(為false)
Jan 19, 2022:::success :smile: 標題取自於靈感取自於線上講座 - 你所不知道的 C 語言 ::: Null and free/delete 在有GC語言特性的程式語言中,如: C#、Java、Golang,若要清空一個變數的值,會類似這樣寫法: MyClass name = new MyClass(); name = null;
Jan 19, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up