Question on constants

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

Simon Harvey

Hi,

Is there any benefit to declaring variables as constants other than not
being able to overwrite them by mistake?
Thanks
Simon
 
Hi,

I guess Constants get replaced with there value while compiling which helps
fast execution.
 
Beware, though: I've read elsewhere that if you define a constant (as in a
const), and then use that constant as a parameter to a method call, that the
constant value (rather than a reference to the constant) will be encoded
method call. That's no big deal if you build all of your code at once, but
if the constant is in a shared shared, and you change its value in the
future, it won't necessarily "take" for existing, possibly deployed,
assemblies that use the constant value in a method call.
 
This is true. Declaring a variable const has that implication.

In fact, every reference made to a "const" variable, will be replaced with
the value of the variable when the code is compiled. Not just method calls.

This has some reprecussions when other people depend on your code. Take the
following scenario. Company A's product depends on your dll which contains
constant definitions. They ship their product built against version 1 of
your dll. Then you ship version 2 of the dll in which some of the
constants' values have changed. Because Company A's product was compiled
against version 1 of your dll, they are still using the constant values from
version 1. This could potentially cause their app to break.

--
Jared Parsons [MSFT]
(e-mail address removed)
This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"
 
Jared Parsons said:
This is true. Declaring a variable const has that implication.

In fact, every reference made to a "const" variable, will be replaced with
the value of the variable when the code is compiled. Not just method calls.

This has some reprecussions when other people depend on your code. Take the
following scenario. Company A's product depends on your dll which contains
constant definitions. They ship their product built against version 1 of
your dll. Then you ship version 2 of the dll in which some of the
constants' values have changed. Because Company A's product was compiled
against version 1 of your dll, they are still using the constant values from
version 1. This could potentially cause their app to break.

And this is why we have strong versioning, of course - Company A's
product should use version 1 of the DLL whether or not a version 2 is
also present.
 
Another alternative.

Company A use a private copy of Company B's dll. That way when a global
update occurs (say in the GAC), Company A is not broken by the changes
Company B makes to their dll.

Then again, they may also be missing security updates.

--
Jared Parsons [MSFT]
(e-mail address removed)
This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"
 
Back
Top