By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,675 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,675 IT Pros & Developers. It's quick & easy.

Why aren't arrays of constants equivalent to constant expressions?

P: n/a
This statement is legal C#:

public const string day = "Sunday";

but this statement is not:

public const string[] days = new string[] { "Sun", "Mon", "Tue" };

The C# Programmer's Reference specifies that 'the only valid values of a constant declarator [involve] constant expressions'. A constant expression is defined as 'an expression that can be fully evaluated at compile-time ... Therefore, the only possible values for constants of reference types are string and null.'

In other words, constant arrays of any otherwise constant type are not legal C#. I understand that this is the case because string[] is really of type System.Array, which is an object (also a dead giveaway since we used the new operator). Fine. I'll get the next best thing and use static readonly in place of where I'd like to use const. The statement becomes:

public static readonly string[] days = new string[] { "Sun", "Mon", "Tue" };

and the compiler's happy.

But now if I run FxCop on an assembly containing this statement, it gets mad at me:

'... Fields that are arrays should not be readonly'
'Make the array 'const' to truly protect its contents.'

What the heck? Am I missing something here? How do you declare a constant array? Any help would be greatly appreciated. Thanks.
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
100
Hi JJ,
I belive this is FxCop mistake. FxCop is right in one, though - declaring an array as readonly doesn't protect your array of changing its items. Consider this

I always can do

someobj.days[0] = "Fri";

what you protect against is that I cannot make
someobj.days = new string[]{"Wed", "Thu", "Fri"}

So *constant* is not fully covered by *readonly*

But this is the best you can do about the arrays at the moment, I believe. FxCop gives you wrong advice.

If you want more constantness you can inherit your own class from CollectionBase let say and make it readonly strongly-typed collection. Then make an object of this collection visible to the rest of the classes via readonly data member or get-only property.

HTH
B\rgds
100
"JJ Feminella" <external#jjnexus@com> wrote in message news:um**************@tk2msftngp13.phx.gbl...
This statement is legal C#:

public const string day = "Sunday";

but this statement is not:

public const string[] days = new string[] { "Sun", "Mon", "Tue" };

The C# Programmer's Reference specifies that 'the only valid values of a constant declarator [involve] constant expressions'. A constant expression is defined as 'an expression that can be fully evaluated at compile-time ... Therefore, the only possible values for constants of reference types are string and null.'

In other words, constant arrays of any otherwise constant type are not legal C#. I understand that this is the case because string[] is really of type System.Array, which is an object (also a dead giveaway since we used the new operator). Fine. I'll get the next best thing and use static readonly in place of where I'd like to use const. The statement becomes:

public static readonly string[] days = new string[] { "Sun", "Mon", "Tue" };

and the compiler's happy.

But now if I run FxCop on an assembly containing this statement, it gets mad at me:

'... Fields that are arrays should not be readonly'
'Make the array 'const' to truly protect its contents.'

What the heck? Am I missing something here? How do you declare a constant array? Any help would be greatly appreciated. Thanks.
Nov 15 '05 #2

P: n/a
for 10*@100.com

Sorry for the injection of this post but this I am trying to get a hold of
10*@100.com to discuss the MVP program and was hoping they could email me at
ra******@microsoft.com. Thanks

--
Rafael M. Munoz
Microsoft PSS Global Community
MVP Lead - .NET / VB / VC# / VJ#

This posting is provided 'AS IS' with no warranties, and confers no rights.
Microsoft Strategic Technology Protection Program for information -
http://www.microsoft.com/security.

---------------------
"100" <10*@100.com> wrote in message
news:um**************@TK2MSFTNGP11.phx.gbl...
Hi JJ,
I belive this is FxCop mistake. FxCop is right in one, though - declaring an
array as readonly doesn't protect your array of changing its items. Consider
this

I always can do

someobj.days[0] = "Fri";

what you protect against is that I cannot make
someobj.days = new string[]{"Wed", "Thu", "Fri"}

So *constant* is not fully covered by *readonly*

But this is the best you can do about the arrays at the moment, I believe.
FxCop gives you wrong advice.

If you want more constantness you can inherit your own class from
CollectionBase let say and make it readonly strongly-typed collection. Then
make an object of this collection visible to the rest of the classes via
readonly data member or get-only property.

HTH
B\rgds
100
"JJ Feminella" <external#jjnexus@com> wrote in message
news:um**************@tk2msftngp13.phx.gbl...
This statement is legal C#:

public const string day = "Sunday";

but this statement is not:

public const string[] days = new string[] { "Sun", "Mon", "Tue" };

The C# Programmer's Reference specifies that 'the only valid values of a
constant declarator [involve] constant expressions'. A constant expression
is defined as 'an expression that can be fully evaluated at compile-time ...
Therefore, the only possible values for constants of reference types are
string and null.'

In other words, constant arrays of any otherwise constant type are not legal
C#. I understand that this is the case because string[] is really of type
System.Array, which is an object (also a dead giveaway since we used the new
operator). Fine. I'll get the next best thing and use static readonly in
place of where I'd like to use const. The statement becomes:

public static readonly string[] days = new string[] { "Sun", "Mon",
"Tue" };

and the compiler's happy.

But now if I run FxCop on an assembly containing this statement, it gets mad
at me:

'... Fields that are arrays should not be readonly'
'Make the array 'const' to truly protect its contents.'

What the heck? Am I missing something here? How do you declare a constant
array? Any help would be greatly appreciated. Thanks.
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.