I'm a long time C programmer, but I'm only starting to learn C#, so forgive me if this is question that should be intuitively obvious.

In C, I dislike 'magic numbers' in code, such as this:

var_name = 1234; /* what is the significance of this value??? */

So instead, in C, I write something like this:

#define meaningFullName 1234

var_name = meaningFullName;  /* this should be more readable*/

If that symbol is useful across other files in my C project, I stick it in a header file, so I can use it where ever I need it.

#include constants.h

var_name = meaningFullName ; /* defined in constants.h */

As I understand it, I can't do that in c#, since #define is used for conditional compilation, and c# doesn't have #include, so I can't use a header file.

#define DEBUG            /* DEBUG = true */

#if DEBUG
     do_something();    /* this will be executed */

#undef DEBUG            /* DEBUG = false  */
     do_this();               /* this won't be executed */

So, how do I define symbolic constants in c# as I am used to doing in c, since #define has an entirely different meaning, and I can't #include a header file?

I found an answer to my question on another forum - reproducing the answer here:

They have to be defined in a class

public class MyClass
{
    public const int myNumber = 1234;
}

To access it, I write:

int x = MyClass.myNumber;

The C# compiler will replace the MyClass.myNumber with 1234 in the generated code. Public/Private/Internal/Protected rules apply. Any class that can access MyClass will be able to access this value.

Now, what that might involve, I'm not sure yet. Still learning... :icon_cool:

commented: Posting Answers to your own questions helps other people searching for answers find them too. thanks :) +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.