Click here to Skip to main content
14,640,713 members
Rate this:
Please Sign up or sign in to vote.
See more:
Generally I have confused about constant,readonly varibles thats why i had search on google refer all the links whatever the given those are told constant,readonly varibles contain fixed values but readonly varbles are changed during the runtime but constant varibles are not changed during the runtime . constant intalized at declartion .it is mandatory but readonly not mandatory to intalize the varible at declartion that should be intalized under constructor also only but how should be same both constant

What I have tried:

Generally I have confused about constant,readonly varibles thats why i had search on google refer all the links whatever the given those are told constant,readonly varibles contain fixed values but readonly varbles are changed during the runtime but constant varibles are not changed during the runtime . constant intalized at declartion .it is mandatory but readonly not mandatory to intalize the varible at declartion that should be intalized under constructor also only but how should be same both constant
Posted
Updated 24-Feb-17 10:46am
v2
Comments
johannesnestler 28-Feb-17 9:17am
   
Google what? just read the c# docu - did you ever do any programming? It's your x'th question here about this topic - in that time I would have tested all possible cases in a quick console application and found out how the keywords behave without any documentation...
Rate this:
Please Sign up or sign in to vote.

Solution 1

They aren't the same.
A const value is defined at compile time and can never be changed.
A readonly value is not defined at compile time - it is set only in the appropriate class constructor and can be a different value each time the code is run. Once set, the rest of the code cannot change the value, so it's a "settable constant" which allows you to (for example) read a value from a database instead of defining it at compile time, and treat it as a constant from there on.

Quote:
I can't understand about readonly


Think about it: pi is a constant - 3.1415927.... it is the same value for every calculation you make, regardless of where in the universe you try to apply it.
So is your date of birth - it never changes for your entire lifetime. But ... your birthday is not the same as mine, which also never changes.

pi is a const value: you define it at compile time and it never changes.
But "dateOfBirth" is different, you can't change it, but it is fixed for each person: it's readonly because you define the actual value when you create an instance of a person, and can never change it again.

So a const value is fixed from the moment you compile your application, and to change it you must recompile; while a readonly value is fixed only when the app runs and sets the value in the class constructor.

Does that make sense?
   
v2
Comments
Krishna Veni 24-Feb-17 16:40pm
   
I can't understand about readonly
OriginalGriff 24-Feb-17 17:16pm
   
Answer updated.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Quote:
i had search on google refer all the links

So you had read the 198000 answers given by Google ? And you got nothing.
You are approaching 80 questions and you are still asking questions about the definition of C#. Do you plan to learn C# only by asking questions here ?

It is a computer language, if you have 2 keywords with different usages, it is probably because they are not the same thing. The manual contain definition for both keywords, with little luck you will have examples.

May be you should read seriously C# manual to learn it ?
   
Comments
OriginalGriff 24-Feb-17 17:17pm
   
Ah. I hadn't realized this was a Help Vampire: upvoted.
Graeme_Grant 24-Feb-17 17:23pm
   
Nice name for it ;)
Patrice T 24-Feb-17 19:26pm
   
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100