Where do we initialize constant members of class in C#?? In C++ we use Data Members Initialization List. But in C# do we have such concept??
Printable View
Where do we initialize constant members of class in C#?? In C++ we use Data Members Initialization List. But in C# do we have such concept??
Constants can either be declared inside a class or a function. If inside a function, the constant can only be used inside that function. If in a class the constant can be used from anywhere (if marked as public).
They are initialized when declared.
Code:public class myClass
{
public const string ConstantStrValue = "some const string";
public const int ConstantIntValue = 100;
}
Just a little copy-edit ;)Quote:
Originally Posted by Arjay
Also, you can't declare constants inside a function, not that i know of anyway.
You can declare constants in a class method such as the constructor however I see no reason to do so and think of it as a bad practice but it is allow see the .NET documentation for the constant keyword in C# they have an example of declaring a method level constant.
All other constants that are to be utilized in the class are declared a constant field as pointed out before.
They do not have to be marked as public and can be marked as private.
It would be inappropriate to create a public instance of a constant field if it were to not be utilized outside of the object's methods.
Only mark constants on values that you be 100% certain will never change during the duration of your applications development life cycle. For all other things that are considered a constant but could be changed by a configuration setting use the web.config or app.config file and create a key to hold this value that way if they ever are changed during setup you will not have to recompile your application.
Thanks, I fixed it.Quote:
Originally Posted by Mutant_Fruit
Just small but important addition:
1. Constats are initialized at compile-time, so they can be only of types which value are know at this time, like numbers and strings. You cannot declare constant of reference type, you have to use static readonly rather.
2. Constants are not referenced, but copied directly to place where they are used. If you change a constant value, you have to rebuild the whole solution and all project where is is used. So be careful of using constant, especialy as public members of class libraries.
Or, if you want to initialize the constants at runtime you can use the readonly attribute and initialize the constants in the constructor.
http://msdn2.microsoft.com/en-us/lib...b7(VS.71).aspx
Just a minor side note, but I've always learned that constants should be written in full caps.
Actually that is the old way of doing things not to say that it is wrong but with the VS IDE there is no reason really to write constants in all caps since you can easily mouse over the type and see that it is a constant.
All the .NET framework documentation marks constants just like any other field in camel case example firstName.
However it all boils down to how your team is use to identifying coding standards.
As long as it is consistent there is nothing wrong with all caps separated by underscores. I don't like looking at code this way but as long as the team is consistent then it is fine.
I prefer constants in Pascal case. I generally prefix my field names with an underscore as well since I like to distinguish between a local variable and a class field (ie. _firstName). I find writing 'this.' everywhere effects my sense of style. My attitute is probably a holdover from C++, but I feel since the field is already a member of 'this' class, there is no need to explicitly write 'this.' everywhere when referencing a class member. But this is strickly my preferences.