Why does C# disallow readonly local variables?

后端 未结 13 1243
萌比男神i
萌比男神i 2020-11-27 17:17

Having a friendly debate with a co-worker about this. We have some thoughts about this, but wondering what the SO crowd thinks about this?

13条回答
  •  旧巷少年郎
    2020-11-27 18:00

    use const keyword to make read only variable.

    reference: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/const

    public class SealedTest
    {
        static void Main()
        {
            const int c = 707;
            Console.WriteLine("My local constant = {0}", c);
        }
    }
    

提交回复
热议问题