Alex Rivera | Logout

How to define constants in Visual C# like #define in C?

Asked 2009-11-27T09:57:32.377
43

In C you can define constants like this

#define NUMBER 9

so that wherever NUMBER appears in the program it is replaced with 9. But Visual C# doesn't do this. How is it done?

Edit
Report

2 Answers

77
public const int NUMBER = 9;

You'd need to put it in a class somewhere, and the usage would be ClassName.NUMBER

answered 2009-11-27T09:59:14.700
16

Check How to: Define Constants in C# on MSDN:

In C# the #define preprocessor directive cannot be used to define constants in the way that is typically used in C and C++.

answered 2009-11-27T10:02:18.073

Your Answer