Differences between readonly and const in ASP. NET, readonlyconst

Source: Internet
Author: User

Differences between readonly and const in ASP. NET, readonlyconst

Const is a keyword used to modify constants. It limits that a variable cannot be changed. Using const can improve the security and reliability of the program to a certain extent. It plays a very important role in programming and brings convenient applications to developers.
Create a console applicationTest:

Public class Test {public readonly string name = "George"; public const string coname = "ABC Company LLC"; public Test (string name) {// The variable modified by readonly can be changed only in Constructor (Constructor) this. name = name;} public string _ name {get {return name;} // you cannot perform the Set operation on the variable modified by readonly. // set // {// name = value; // }}} class Program {static void Main (string [] args) {Test obj = new Test ("Test"); // The value of the readonly variable cannot be modified, it can only be changed in Constructor (Constructor) // obj. name = "New Value"; Console. writeLine (obj. name); // The const variable is directly accessed through the object without instantiating the Console. writeLine (Test. coname); Console. read ();}}

I used to think that readonly and const serve the same purpose. Now I understand the differences between them. I wonder if you understand them too? Hope you will get something!

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.