Web Design that taps into the haromny and vision of your dreams.

C# Constants and Read-Only Variables

How to use C# Constants and Read-Only Variables

Written By on in Coding

591 words, estimated reading time 3 minutes.

C# constants are variables that hold static data and should be used anywhere that you would hard code static values. This allows for easier code maintenance and debugging as well as providing a central location for these values.

C# Programming Series
  1. Introduction to Programming
  2. What is C#?
  3. Your First Console Application in C#
  4. Introducing Methods and the Main() Function in C#
  5. Introducing C# Classes and Structs
  6. C# Data Types, Variables and Casting
  7. C# Program Flow Control and Entry Points
  8. Passing Parameters to Methods and Return Values in C#
  9. C# Access Modifiers and Scope
  10. C# Interfaces and Classes
  11. Using Namespaces in C#
  12. C# Conditional Statements
  13. Looping and Iteration in C#
  14. Using Arrays and Lists in C#
  15. C# Constants and Read-Only Variables
  16. Error and Exception Handling in C#
  17. Using Recursion in C#
  18. C# Operator List
  19. Class Inheritance in C#
  20. C# Class and Method Attributes Explained
  21. C# Class Constructors and Destructors
  22. C# Generics Variables
  23. XML Serialization and Deserialization
  24. C# String Formatting Examples

C# Constants and Read Only variables perform the same task, the only difference between them is that a constant is given a value at compile time and cannot ever be changed. Read Only variables can be assigned to once at runtime, but thereafter cannot be changed.


Constants are declared using the const keyword and can be used as class members or as method values and can be marked with any access modifier apart from static. Constants should be used anywhere where you would hard code a value, such as a number of results to return, literal strings, loop control, mathematical constants, fixed costs and so on.

public const decimal pi = 3.14M;
private const int numRows = 10;
private const decimal vatRate = 17.5M;

This will define a value for pi which will never change once the program is compiled; similarly, numRows will be initialised to 10 and will never change. If you try to assign to a const value the compiler will throw an error.

Read Only

Read only values allow one assignment at runtime and can be used to hold information that needs to be read in or processed. Once a read-only value has been assigned to once it cannot be assigned to again until the application is restarted.

public class SomeTestClass
  private readonly string filePath;
  public SomeTestClass(string filename)
    filePath = Path.GetDirectoryName(filename);

This will initialise the value filePath to the directory of the specified filename. Once the constructor has assigned to filePath it can no longer be assigned to.

Using Consts and Read Only Values

Let's assume you are creating a class to deal with calculations involving circles. You create the class using hard coded values for pi.

public class Circles
  public double Circumference(double radius)
    return circumference= 3.14 * (radius * 2);
  public double Area(double radius)
    return area = 3.14 * Math.Pow(radius, 2);
  public double AnotherTest(double radius)
    if (radius <= 3.14)
      return radius * 3.14 * Math.Pow(3.14, 3);
      return radius * Math.Pow(3.14, 4);   

You can imagine that this class could contain many more methods and constants. During testing, it has been found that the calculations performed do not have the accuracy required. It is necessary to improve accuracy by changing the value of pi from 3.14 to 3.1415926. How do you accomplish this?

You could do a search and replace, but how do you know that 3.14 is always the value of pi? The AnotherTest method does not use pi, but another fixed value that has the same value. Changing this value could result in undesired results.

It would be much better to have created the class using constants which would have the following benefits:

  1. The value of pi needs only to be changed once.
  2. You can be sure that you haven't missed any values.
  3. The code looks neater, and is easier to understand.
  4. pi value 3.14 is not confused with another value of 3.14
public class Circles
  private const double pi = 3.1415926;
  private const double test = 3.14;
  public double Circumference(double radius)
    return circumference= pi * (radius * 2);
  public double Area(double radius)
    return area = pi * Math.Pow(radius, 2);
  public double AnotherTest(double radius)
    if (radius <= test)
      return radius + test * Math.Pow(pi, 3);
      return radius * Math.Pow(pi, 4);   

From this, you can see that it is easy to see that the values for test and pi are totally different in the AnotherTest method. Also, to change the value of pi, simply change one line of code at the top of the class.

Of course in the real world, you would use Math.PI for the value of pi as this figure is an even more accurate predefined constant.

Last updated on: Thursday 22nd June 2017



There are no comments for this post. Be the first!


Leave a Reply

Your email address will not be published.

If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.


Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me