Skip to content

Difference between static, const, read-only and static read-only

kalit sikka

Asked Jul 8, 2006, 2:36 AM EDT1 answer
Share
LinkedInX
Im not clear with difference between static variable, const variable read-only variable and static readonly variable. Is there is any logical difference between them.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Mahesh Chand
    0
    Static - Static objects can have only a single memory allocation. All callers share the same memory. It is useful when we know an object is going to have a static data. For example, if I have a connection string in an application, I know the connection string is not going to change no matter how many callers use it. So I would create a static property or method, that will return connection string. Think of it like a Cigar being shared among bunch of guys.

    Non Static - Each object has it's own memory space. This is like each guy has it's own cigar ;-)

    Static class - The entire class is static, means has a single memory space allocated for it.

    Read-only - Can not modify the value of the member. Can read it only.

    Const - It's a variable you can not modify. They are used to give a better readable name to a constant value.

    Static read-only - Static + read only ;)

    Hope it helps.
    0