I've always been in the habit of defining constants at the class level:
public class MyClass
{
private const int SomeNumber = 10;
...
}
But, I recently worked with someone who believes that if a constant is only used by a specific method it should be defined in that method:
public class MyClass
{
public void SomeMethod()
{
const int SomeNumber = 10;
...
}
}
My only argument for preferring the first is that if the constant is needed in other methods there is no refactoring needed and also it makes it easier to update constants since they'll be defined in the same place.
Are there any other pros/cons or is there no real difference?
If it needs updating it's not really a constant. I mean, you don't often refactor the value of Pi. Things that change belong in a configuration file in my opinion.
That said, I usually use class level public constants. I've never seen or used method-level constants. Conceptually, a method level constant makes little sense to me.