1

Short Version

In C++ I could do this...

const auto & help_id = help_type.functionHelpTypeId;

How can I do that in C#?


Long Version

How can I make one variable be a readonly alias for a class field or property in C#?

I have a method where I am making read-only aliases at the top of my function in order to make the rest of the code a little clearer and save some typing later.

        var help_id = help_type.functionHelpTypeId;
        var changeset_version = changeset.version;
        var helpPageType = help_type.helpPageType;
        // &ct.

My problem is that since these are not const, anyone could reassign them or mutate the objects they're referring to, which is not my intention. In order to know that these are just readonly aliases for class fields, one would basically have to study the whole function, which isn't ok.

QuestionC
  • 10,006
  • 4
  • 26
  • 44
  • 1
    Short answer: You can't. Long(er) answer: You can use `readonly` if you set the values in your object constructor. – DavidG Aug 17 '16 at 16:29
  • 1
    What is wrong in using `const` and explicit type declaration ? Have you tried that? `const int help_id = help_type.functionHelpTypeId;` – Shyju Aug 17 '16 at 16:30
  • By "anyone could reassign..." do you mean that you're worried about the maintainers of your code screwing up? You could add a code comment where you declare and assign the variables. – adv12 Aug 17 '16 at 16:30
  • 5
    Side note: if your method soooo looooooong that you need to protect local variables that way you may want to refactor the code into smaller methods. – Alexei Levenkov Aug 17 '16 at 16:32
  • This isn't about code length so much as dealing with labyrinthine field and type names, and honestly expecting C# to be as expressive as C++. – QuestionC Aug 17 '16 at 16:42

2 Answers2

2

You can't: "Implicitly-typed local variables cannot be constant".

Just use explicit type for const.

More discussion: Type-inferring a constant in C#

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
1

To complement @AlexeiLenkov's answer, you can still create a anonymous type whose properties are readonly:

var help = new
{
    help_id = help_type.functionHelpTypeId,
    changeset_version = changeset.version,
    helpPageType = help_type.helpPageType,
};

help.help_id = 312; // CS0200: It is readonly

However, this can only be used within local scope. Also, take note that reference types are mutable, and help itself is also a reference type.

Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
  • the `help` variable can still be mutated. The value it references can't be, but the variable itself can be. – Servy Aug 17 '16 at 17:11
  • @Servy I didn't say it wasn't. In fact, it is even implied that it is mutable, as `help` is a reference type. Perhaps, I should reword that to make it clear. – Xiaoy312 Aug 17 '16 at 17:17
  • You said that the type is mutable, when it's not, and you provided a mutable variable, when the question specifically asks for a variable that *isn't* mutable. – Servy Aug 17 '16 at 17:27