Difference between static and constant
What is a variable ?
A variable, its value can be changed by the program at runtime. The accessibility or the scope of a variable refers to where the variable can be read from or written to, and its lifetime, or how long it stays in the computer memory.
e.g.
Constant
A Constant is something that will always remain the same though out the entire lifetime of a program. A Constant variable cannot be modified after it defines and it cannot be change throughout the program. The Constant with a fixed value tells the compiler to prevent the programmer from modifying it. Whenever you try to change it, it will throw an error message. Constant variables declares with const keyword and can be used with primitive data types . Constants are set at compile time itself and assigned for value types only.
e.g.
Static
Static variable is a property of a Class rather than the instance of class. It is stored on the data segment area of memory and the same value is get shared to all instances of that class. It can be assigned for reference types and set at run time. The static modifier can be applied with classes, fields, methods, properties, operators, events and constructors. It represent a kind of a global value for all the instances of that class and can able to call them using class name.
Where Product is class name and price is a static declaration.
- Difference between NameSpace and Assembly
- Execution process for managed code
- What is an Asssembly Qualified Name
- How to Assembly versioning
- Add and remove an assembly from GAC
- What is Native Image Generator (Ngen.exe)?
- How to force Garbage Collection
- Difference between CType and DirectCast
- Obsolete or Deprecated in NET Framework
- What is lazy initialization
- Difference between Clone and Copy
- Difference between Finalize() and Dispose()