Is there a C# equivalent to using #define in c++ I know I can use enumerations for integer constants that I need, and I assume the actual enumeration wont be compiled and stored in my app, but what about floating point constants
Well, as far as I am aware, those statements will compile into code, thus wasting space in my exe. Now, I know you think I'm penny pinching here, but if I have a LOT of numbers to define, then the problem starts to become real.
#define is C++ is a neat way to name inline constant numbers, but C# seems to be lacking such a space saving feature.
Unless of course the optimising compiler does something similar...
#define TEST
using System;
public class MyClass
{
public static void Main()
{
#if (TEST)
Console.WriteLine("TEST is defined");
#else
Console.WriteLine("TEST is not defined");
#endif
}
}
Constants are the way to go for you. The problem you're describing with size of assembly should only disturbe you if you're working with thousands and millions of constants, which will bloat the assembly.
If you want, there's an alternate path you might want to explore: Create an assembly that has all the constants in it, reference it from your assembly and then compile. The constant values will be copied to your code and since the constants' assembly will never be used, it will never be looked for, allowing you to simply not distribute it.
I would only recommend this after some very extensive testing, as I'm not sure all JIT implementations will actually allow this.
C++ #define equivalent
Shahedul Huq Khandkar
Well, as far as I am aware, those statements will compile into code, thus wasting space in my exe. Now, I know you think I'm penny pinching here, but if I have a LOT of numbers to define, then the problem starts to become real.
#define is C++ is a neat way to name inline constant numbers, but C# seems to be lacking such a space saving feature.
Unless of course the optimising compiler does something similar...
Agony
const int CONSTVALUE = 10;
int i = getsomevalue();
i = i + CONSTVALUE;
i = 10 * CONSTVALUE;
will become this when compiled.
int i = getsomevalue();
i = i + 10;
i = 100;
If you want to know what some piece of code actually turn into download and look at the compiled code with reflector.
Jason Perez
You can use #define in C#:
#define TEST using System; public class MyClass { public static void Main() { #if (TEST) Console.WriteLine("TEST is defined"); #else Console.WriteLine("TEST is not defined"); #endif } }Mark Paj...
If you want, there's an alternate path you might want to explore: Create an assembly that has all the constants in it, reference it from your assembly and then compile. The constant values will be copied to your code and since the constants' assembly will never be used, it will never be looked for, allowing you to simply not distribute it.
I would only recommend this after some very extensive testing, as I'm not sure all JIT implementations will actually allow this.
Hope this helps.
gijshompes
c# only defines tokens, not constants.
Edit: sorry, 'symbols' they are called.
It's the same adding '/define:TEST' as a compiler option
Geeshan
what about
const float First = 1.0f;
const float Second = 2.0f;