1、應用場景重現
一個簡單的應用程序解決方案,如下:

其中,類庫CSharpLib裡定義一個簡單的類SomeType如下:
1
namespace CSharpLib
2
{
3
public class SomeType
4
{
5
public const int ConstField = 50;
6
public static readonly int ReadonlyField = 50;
7
}
8
}
在控制台應用程序ConsoleApp中,引用類庫CSharpLib,然後寫下如下代碼:
01
using System;
02
03
namespace ConsoleApp
04
{
05
using CSharpLib;
06
07
class Program
08
{
09
static void Main(string[] args)
10
{
11
Console.WriteLine("Const field is {0}.", SomeType.ConstField);
12
Console.WriteLine("Readonly field is {0}.", SomeType.ReadonlyField);
13
Console.ReadKey();
14
}
15
}
16
}
這樣這個控制台應用程序的輸出就都是50,這個結果應該是每個開發人員都預期的,沒有任何可疑之處。
當我們把類庫CSharpLib中的常量都改變時:
1
namespace