Is it possible to delay the initialization of a constant?

Asked

Viewed 150 times

-6

With delay initialization, I say initialize a constant after your declaration. For example (pseudocode):

const exemplo;
exemplo = 2;
  • 2

    I don’t think the question is entirely bad, but it could be improved. I voted to close as insufficiently clear because the author himself answered and did not seem to have understood the question itself.

  • I left my reply as community Wiki.

  • 1

    What does it interfere with? Do you agree with the points I raised in the reply?

  • Just a warning. Yes, I agree with some since certain issues are not within the context of the question

1 answer

6

I answered because I think there is genuine interest and many can learn from the content here, and I only did so because I had a bad answer, so either it was to erase everything so as not to give wrong information or contest what had been written. There have been several issues in the question and answer and both continue with the problems I present here, although now less, the fact that acceptance is made in a wrong answer shows that there was no interest of the AP in learning the correct, a pity, but it is necessary to make this caveat because after so many editions some things that I wrote here may seem out of context and I will not be monitoring every issue trying to tidy up without getting, the answer even got worse and nor answer what was asked.

Either it is variable or it is constant (there was this confusion in the original question, corrected a part, but not everything). Maybe it meant that the identifiers for a constant value can be initialized after your declaration.

So if testing what has been proposed (in fact it is not well known what has been proposed to test in the original answer) in some languages will not work as expected by AP/R, these languages do not even have this concept of variable that can no longer change its value.

The question is not good because in each language has a different behavior of this construction, even never seen a language with this syntax const var (I had in question).

If it is a real constant you have to initialize immediately and with a value solved at compile time, in some cases what can be used is even more limited. So you’re just saying what value will be used instead of this identifier. Since C# was quoted in the answer would be with const.

In C# a constant does not have a storage location, while a read-only variable has a storage location like any variable.

If you use a readonly in C# is not creating a constant, is creating a variable that will not have its value modified under normal conditions after initializing.

The reason for being able to initialize afterwards is precisely because it is not constant, no wonder that the attribute name of the statement is not const and yes readonly, because you can only write once and then just read.

One of the advantages of this is that you can use values created at runtime, and so you don’t need to initialize in the code, because as it will have a value in each instance, it doesn’t have to be a de facto constant, only an immutable variable.

It is also an error to think that you can initialize when you want, it can only be initialized in the constructor (initialize it inline next to the transponder on the boot background is transported to the builder).

This only applies to fields, at the moment C# does not have this for local use (within methods), or has only partially (parameter) and has proposals to have variables explicitly immutable. This distinction is important.

  • Anyone who’s negative could tell me what’s wrong so I can get better. The negatives of the other were passed by me and Woss, I imagine that the other three must believe that they have the same mistakes, I do not know the mistake that has in mine to deserve negative and it would be good for all to learn.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.