On Sun, 01 Feb 2004 14:11:13 +0000, C# Learner <cs****@learner .here>
wrote:
Should constant class member variables be held like this:
public class Foo
{
public const int ConstVar = 100;
}
Or like this:
public class Foo
{
public int ConstVar
{
get { return 100; }
}
}
Or even:
public class Foo
{
private const int constVar = 100;
public int ConstVar
{
get { return constVar; }
}
}
Since I've been using C# and .NET (about two weeks), I've been using
the first way, i.e. public member variables.
Which is the "correct" way to do this, according to common style?
I would say that the first one is the correct and most performant one.
In the second example you expose the constant as a property, which is
overhead. A property should be used when some extra code is to be
executed before returning the value. In the case of a constant I don't
see the need for this.
In the third example you put the constant private, to hide it. I don't
think there's need to do this, for the samen reason as above. The
constant cannot be changed anymore, private or public.
My 2 cents,
Ludwig