Const and readonly usage of c #

Source: Internet
Author: User

1. Application Scenario Reproduction

A simple application solution is as follows:

The CSharpLib class library defines a simple SomeType class as follows:

1 namespace CSharpLib 2 { 3      public class SomeType 4      { 5          public const int ConstField = 50; 6          public static readonly int ReadonlyField = 50; 7      } 8 }

In the ConsoleApp of the console application, reference the class library CSharpLib and write the following code:

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 }

In this way, the output of this console application is 50, and the result should be expected by every developer, without any suspicious points.

When we change the constants in the class library CSharpLib:

1 namespace
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.