none
Guideline where to use const?

    Question

  • Is there a best practice where to put const? e.g. should it always be a class member? or can it be method const only?

    What is the pros and cons, etc.? Any pointers are appreicated.

    Thanks.

    Monday, May 18, 2009 10:50 PM

Answers

  • Basically, you'll want to set a variable constant any time it represents a constant value.  If your variable is 3, and will always be 3, it should be a const.

    It really doesn't matter at what scope you're using the variable - that's not relevant.  You can have const members at the (static) class level, class instance level, or method level.  It's more about how the variable is going to be used, not where it is located.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Figo Fei Wednesday, May 20, 2009 9:03 AM
    Monday, May 18, 2009 11:05 PM
    Moderator
  • One thing I'd like to mention is the differences between 'const' and 'readonly', Patrick has done a summary http://weblogs.asp.net/psteele/archive/2004/01/27/63416.aspx:
    'const':
    • Can't be static.
    • Value is evaluated at compile time.
    • Initiailized at declaration only.
    'readonly':
    • Can be either instance-level or static.
    • Value is evaluated at run time.
    • Can be initialized in declaration or by code in the constructor.

    Please especially note the bold words, and for more information please check the C# language reference of 'const': http://msdn.microsoft.com/en-us/library/e6w8fe1b.aspx

    Thanks.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
    • Marked as answer by Figo Fei Wednesday, May 20, 2009 9:03 AM
    Wednesday, May 20, 2009 9:02 AM

All replies

  • Basically, you'll want to set a variable constant any time it represents a constant value.  If your variable is 3, and will always be 3, it should be a const.

    It really doesn't matter at what scope you're using the variable - that's not relevant.  You can have const members at the (static) class level, class instance level, or method level.  It's more about how the variable is going to be used, not where it is located.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Figo Fei Wednesday, May 20, 2009 9:03 AM
    Monday, May 18, 2009 11:05 PM
    Moderator
  • One thing I'd like to mention is the differences between 'const' and 'readonly', Patrick has done a summary http://weblogs.asp.net/psteele/archive/2004/01/27/63416.aspx:
    'const':
    • Can't be static.
    • Value is evaluated at compile time.
    • Initiailized at declaration only.
    'readonly':
    • Can be either instance-level or static.
    • Value is evaluated at run time.
    • Can be initialized in declaration or by code in the constructor.

    Please especially note the bold words, and for more information please check the C# language reference of 'const': http://msdn.microsoft.com/en-us/library/e6w8fe1b.aspx

    Thanks.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
    • Marked as answer by Figo Fei Wednesday, May 20, 2009 9:03 AM
    Wednesday, May 20, 2009 9:02 AM