TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# const Example

Use the const keyword to indicate a constant value. Const values cannot be assigned during runtime.
Const. This keyword indicates a constant. It describes an entity that cannot be changed at program runtime. Instead, the entity must be fully resolved at compile-time.Keywords
Notes, const. We cannot reassign a constant. With constants, we lose the ability to modify variables at runtime but we gain performance and compile-time validation.Optimization
An example. With const we pull constant values (such as strings or ints) into a part of the program that is easier for us to manage and edit. This improves program organization.

Tip: When we compile const values, the values are inserted into the parts of the program that use them—there is no lookup overhead.

Part A: The program accesses the _html constant string in 2 ways. We can read a constant in the same way as a static field.

Static

Part B: Here we show that a constant can be placed in the local scope, like a local variable.

Part C: We cannot reassign consts in a program. This would result in a compile-time error.

C# program that uses const using System; class Program { const string _html = ".html"; static void Main() { // Part A: access constant. Console.WriteLine(_html); Console.WriteLine(Program._html); // Part B: local constant. const string txt = ".txt"; Console.WriteLine(txt); // Part C: invalid statements. // ... This causes a compile-time error. // _html = ""; // ... This causes a compile-time error also. // txt = ""; } } Output .html .html .txt
Left-hand side error. This is what happens when we try to mutate a constant value. We get a C# compile-time error. This program won't go far in life.
C# program that causes left-hand side error class Program { static void Main() { const int value = 10; value = 20; } } Output Error CS0131 The left-hand side of an assignment must be a variable, property or indexer
Constant error. All parts of a constant expression must themselves be constant. So we cannot buildup a const from variables. No variables are allowed.
C# program that causes constant error class Program { static void Main() { int size = 5; const int value = 10 + size; } } Output Error CS0133 The expression being assigned to 'value' must be constant
Errors, notes. When refactoring, we may run into some errors related to constants. A compile-time error will occur if we try to assign constants at runtime after they are already assigned.

Tip: To fix this error, either modify the constant to make it a variable, or remove the assignment.

Advantages. Constants promote the integrity of your programs by telling the compiler not to allow you to change the values during runtime. This should result in fewer accidental bugs.

Quote: Magic numbers are literal numbers, such as 100 or 47524, that appear in the middle of a program without explanation. If you program in a language that supports named constants, use them instead (Code Complete).

Performance notes. Const may improve performance over variables in many cases, particularly when using types like int. It eliminates accesses to memory that occur with fields and locals.Readonly
A summary. Const is a reserved word. It specifies that a value is invariant and must not be modified after compile-time. Const values, like const strings, simplify and optimize programs.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf