otm2000_zhang

In VS2005 C# make a console project
First i write this:
using System;
using System.Collections.Generic;
using System.Text;

namespace TestConst
{
class Program
{
static void Main( string[] args )
{
const string[] e = new string[] { "1", "2", "3" };
}
}
}

compiler failed: says only can use null to initial

Then change to:
using System;
using System.Collections.Generic;
using System.Text;

namespace TestConst
{
class Program
{
static void Main( string[] args )
{
const string[] e = null;
}
}
}

guest what happen Compiler crash!



Re: Common Language Runtime Maybe Big Bug

vtortola

Humm ... if "const" keyword to this use

try:

Code Snippet

using System;
using System.Collections.Generic;
using System.Text;

namespace TestConst
{
class Program
{
static void Main( string[] args )
{
readonly string[] e = new string[] { "1", "2", "3" };
}
}
}

Regards.





Re: Common Language Runtime Maybe Big Bug

otm2000_zhang

Will your compiler crash I only care about this point.




Re: Common Language Runtime Maybe Big Bug

Jonathan Nappee

Yes mine crashes.
Only Primitives can be const. Arrays are not primitives and therefore not standard and behaviourly undefined. However it should not crash and I believe it to be a bug.

From What I read in "CLR via C#" by Jeffrey Richter second edition, defining a constant causes the creation of metadat. Compilers need to look at the symbol in the metadata that defines the constant to get the constant value. I am guessing that the compiler crashes in the access to the symbols value through null.






Re: Common Language Runtime Maybe Big Bug

Martin Xie - MSFT

Yes, mine crashes too.

Generally speaking, the compiler error " 'variable' is of type 'type'. A const of reference type other than string can only be initialized with null." is normal.

As for array in your case, the issue may be a bug. Please submit a bug or suggestion.

Thanks for your support and attention!