How can make a variable (not class member) "read only" in C#

14,054

Solution 1

There isn't an identical analogue.

The readonly keyword allows the variable value to be mutated, but only in a constructor.

The const keyword means the value cannot mutate and needs to be a compile time constant and can only be one of the following types: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, an enum-type, or a reference-type. (C# 4.0 spec §10.4).

And in c#, readonly only applies to fields and cannot be applied to local variables.

Solution 2

No, there exists no solution for your code-example.

const in C# is for compile-time constants and since your variable gets its value from a function, it's not known at compile-time.

The readonly keyword does kind of what you're looking for, but that's only for member variables in classes (and only allows variables to be set in the constructor of the class).

But, on the other hand, why would you ever need it? If your function is very long, it should be refactored into smaller functions. If it's not very long, then why would you need to enforce a rule like this? Just don't assign to readOnlyVar is probably my best suggestion for you I'm afraid.

Solution 3

There are two ways to set a variable as read only.

public class ClassA
{
  private const int I = 5;
  private readonly int J = 5;
}

The const keyword will set the value at compile time. The readonly keyword will set the value at construction. So, if you need different values for each instance, use readonly. Otherwise use const.

Solution 4

in c# we having const or readonly keyword to declare a constant.

const

A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the const keyword and must be initialized as they are declared. For example;

public class MyClass
{
  public const double PI = 3.14159;
}

PI cannot be changed in the application anywhere else in the code as this will cause a compiler error.

readonly

A read only member is like a constant in that it represents an unchanging value. The difference is that a readonly member can be initialized at runtime, in a constructor as well being able to be initialized as they are declared. For example:

public class MyClass
{
  public readonly double PI = 3.14159;
}   

or

public class MyClass
{
  public readonly double PI;

  public MyClass()
  {
    PI = 3.14159;
  }
}

Because a readonly field can be initialized either at the declaration or in a constructor, readonly fields can have different values depending on the constructor used. A readonly field can also be used for runtime constants as in the following example:

public static readonly uint l1 = (uint)DateTime.Now.Ticks;

Notes

readonly members are not implicitly static, and therefore the static keyword can be applied to a readonly field explicitly if required.

A readonly member can hold a complex object by using the new keyword at initialization. readonly members cannot hold enumerations.

credit goes here: http://www.dotnetspider.com/forum/69474-what-final-c-i-need-detailed-nfo.aspx

Share:
14,054
devfreak
Author by

devfreak

Updated on June 26, 2022

Comments

  • devfreak
    devfreak almost 2 years

    I'm new to the C# world and I can't find a method to declare read only variable in C# (something like declaring "const" variable in c++). Is there one?

    I'll give you an example:

    ...
    int f() { return x; } // x is not const member
    ...
    void g() {
        int readOnlyVar = f(); // is there a method to declare readOnlyVar as read only or const
    
        // Some code in which I want to restrict access to readOnlyVar to read only 
    }
    
  • Jahan Zinedine
    Jahan Zinedine over 13 years
    mention that const can not be applied to every data type
  • devfreak
    devfreak over 13 years
    Exactly as I expected, just wanted to be sure, Thanks!