Var vs const | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Var vs const

// can we write ? const int x; x=9;

3rd Jan 2017, 1:52 PM
ali saad soliman
4 Answers
+ 8
No, this will give a compiler error. Constants in C# must be given a value when they are declared.
3rd Jan 2017, 2:09 PM
Lauren
+ 6
No, this will cause an error coz constant must be initialized with a value when declared nd it can't be changed, once it is assigned.
12th Jan 2017, 7:09 PM
Sunil Tiwari
Sunil Tiwari - avatar
+ 3
Just to add to what Lauren said. Constant variables can not be changed during runtime of your program.
4th Jan 2017, 9:15 AM
Andreas BeEm Kvist
Andreas BeEm Kvist - avatar
+ 2
The difference between a constant and a variable is that a variable can change its value (like int x=5; some code x = 9.) But a constant cannot be changed after you assign it to a value.
11th Jan 2017, 5:23 AM
Ivan
Ivan - avatar