-
October 17th, 2007, 07:06 AM
#1
Constant Members
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??
-
October 17th, 2007, 11:57 AM
#2
Re: Constant Members
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).
-
October 17th, 2007, 12:27 PM
#3
Re: Constant Members
They are initialized when declared.
Code:
public class myClass
{
public const string ConstantStrValue = "some const string";
public const int ConstantIntValue = 100;
}
Last edited by Arjay; October 17th, 2007 at 01:19 PM.
-
October 17th, 2007, 12:45 PM
#4
Re: Constant Members
Originally Posted by Arjay
They must be initialized when declared.
Code:
public class myClass
{
public const string = "some const string";
public const int = 100;
}
Just a little copy-edit
Also, you can't declare constants inside a function, not that i know of anyway.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
October 17th, 2007, 01:17 PM
#5
Re: Constant Members
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.
-
October 17th, 2007, 01:20 PM
#6
Re: Constant Members
Originally Posted by Mutant_Fruit
Just a little copy-edit
Also, you can't declare constants inside a function, not that i know of anyway.
Thanks, I fixed it.
-
October 18th, 2007, 06:14 AM
#7
Re: Constant Members
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.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
October 18th, 2007, 07:38 AM
#8
Re: Constant Members
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
Jason Isom
.NET 2.0 / VS2005 Developer
-
October 18th, 2007, 10:07 AM
#9
Re: Constant Members
Just a minor side note, but I've always learned that constants should be written in full caps.
Sincerely,
Martin Svendsen
-
October 18th, 2007, 10:18 AM
#10
Re: Constant Members
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.
-
October 18th, 2007, 11:26 AM
#11
Re: Constant Members
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|