How to make a variable (not a class member) read-only in C #

I am new to C # and I cannot find a method for declaring a read-only variable in C # (something like declaring a const variable in C ++). Is there any?

I will 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 } 
+4
source share
5 answers

There is no identical counterpart.

The readonly keyword allows you to change the value of a variable, but only in the constructor.

The const keyword means that the value cannot mutate and must 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, enum-type or reference-type. (C # 4.0 spec ยง10.4).

And in C #, readonly applies only to fields and cannot apply to local variables.

+10
source

No, there is no solution for your sample code.

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

The readonly keyword does what you are looking for, but it is only for member variables in classes (and only allows you to set variables in the class constructor).

But on the other hand, why did you need this? If your function is very long, it should be reorganized into smaller functions. If this is not very long, then why do you demand compliance with this rule? Just don't assign readOnlyVar , probably my best suggestion for you, I'm afraid.

+4
source

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

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

The const keyword sets the value at compile time. The readonly keyword sets the value in the build. So, if you need different values โ€‹โ€‹for each instance, use readonly. Otherwise use const.

+2
source

in C #, we have a const or readonly keyword to declare a constant.

Const

The constant member is determined at compile time and cannot be changed at run time. Constants are declared as a field using the const keyword and must be initialized as they are declared. For instance:

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

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

only for reading

A read-only member as a constant in that it represents an immutable value. The difference is that the readonly element can be initialized at run time, and the constructor can be initialized as they are declared. For instance:

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

or

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

Because the readonly field can be initialized either in the declaration or in the constructor, readonly fields can have different values โ€‹โ€‹depending on the constructor used. The 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 a static keyword can be applied to the readonly field explicitly if necessary.

The readonly element can contain a complex object using the new keyword during initialization. readonly members cannot contain enumerations.

the loan goes here: http://www.dotnetspider.com/forum/69474-what-final-ci-need-detailed-nfo.aspx

+1
source

This is not a language function, but you are not the only person interested in such a function . However, you have some options. You can replace the implementation of your methods with a class with readonly member variables. This is a big pain, although it really explodes the size of your code. (When you write lambdas or use async methods, C # does something like raising locales to the fields of a class and turns your lambda method or asynchronous method into methods in an auto-generated class. We basically do the same, but manually so we can set readonly .)

 class Scope { int x; int f() => x; // x is not const member void g() { new gImpl(f()).Run(); } class gImpl { readonly int readOnlyVar; public gImpl( int readOnlyVar) { this.readOnlyVar = readOnlyVar; } public void Run() { // Some code in which I want to restrict access to readOnlyVar to read only // error CS0191: A readonly field cannot be assigned to (except in a constructor or a variable initializer) readOnlyVar = 3; } } } 

Another alternative that requires less code, but is still clumsy, is to abuse the foreach function. The foreach keyword does not allow iteration to be assigned to a variable. So you can do something like this:

 class Scope { int x; int f() => x; void g() { foreach (var readOnlyVar in new[] { f(), }) { // Some code in which I want to restrict access to readOnlyVar to read only // error CS1656: Cannot assign to 'readOnlyVar' because it is a 'foreach iteration variable' readOnlyVar = 3; } } } 

The foreach method makes it easier to use anonymous types, but you can use generics in conjunction with type inference to use anonymous types using the class method.

0
source

Source: https://habr.com/ru/post/1334761/


All Articles