473,486 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
2 1799
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
4945
by: E. Robert Tisdale | last post by:
Please find attached the physical constants header file physical.h It defines conversion factors to mks units. It might be used like this: > cat main.cc #include<iostream>...
14
2789
by: John Ratliff | last post by:
I'm trying to find out whether g++ has a bug or not. Wait, don't leave, it's a standard C++ question, I promise. This program will compile and link fine under mingw/g++ 3.4.2, but fails to link...
79
3325
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
11
4429
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
53
4227
by: SK | last post by:
Hi I appreciate all of the feedback I have recieved. I am enjoying working on my program and I hope that it can be appreciated. I have my program compiling now and I am continuing to work out...
21
4148
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
34
3339
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const...
3
5826
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
5
1758
by: Ioannis Vranos | last post by:
==C90: Errata for K&R2 page 232 (from http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html):\ 232(§A12.5): The result of the defined operator is not replaced literally by 0L...
0
7099
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.