tags:

views:

131

answers:

2

My question is similar to http://stackoverflow.com/questions/955418/const-double-initialised-from-lua but I am asking this question

Double W1 = -21.0;
const Double X = (const Double) W1;

is there any way to do so.

And is there any difference between:

    double Y;
    Double Y;
+9  A: 

Is there any way to do so?

There isn't because a constant value has to be known at compile time, so you cannot assign it to a normal variable

And is there any difference between: double Y; and Double Y;

Absolutely no difference, the first is an alias to the actual type.

Darin Dimitrov
...............................................Thanks ......................................
Rahul2047
+2  A: 

If the symbol that you wish to behave as a constant is a field you could use the readonly modifier instead of constant. This will allow you to assign a value to the symbol either in the field initializer or the (static/non-static) constructor.

From MSDN:

The readonly keyword differs from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, although a const field is a compile-time constant, the readonly field can be used for run-time constants, as in this line: public static readonly uint l1 = (uint)DateTime.Now.Ticks;

Example:

class A
{
    readonly double d;

    public A()
    {
        double w = -21.0;
        d = w;
    }
}
0xA3
Actually both the answers seems OK to me ... But here I got new idea.. Thanks.
Rahul2047