How do I create a constant that is an array?

Published 03 December 04 09:33 AM

Strictly speaking you can't, since const can only be applied to a field or local whose value is known at compile time.

In both the lines below, the right-hand is not a constant expression (not in C#).

const int [] constIntArray = newint [] {2, 3, 4};
 // error CS0133: The expression being assigned to 'constIntArray' must be constant
const int [] constIntArrayAnother = {2, 3, 4};
 // error CS0623: Array initializers can only be used in a variable or field
 //               initializer. Try using a new expression instead. 

However, there are some workarounds, depending on what it is you want to achieve.

If want a proper .NET array (System.Array) that cannot be reassigned, then static readonly will do for you.

static readonly int [] constIntArray = new int[] {1, 2, 3};

The constIntArray field will be initialized before it its first use.

If, on the other hand, you really need a const set of values (say as an argument to an attribute constructor), then - if you can limit yourself to integral types - an enum would serve you well.

For example:

[Flags]
public enum Role
{
	Administrator = 1,
	BackupOperator = 2,
	// etc. 
}

public class RoleAttribute : Attribute
{
	public RoleAttribute()
	{
		CreateRole = DefaultRole;
	}

	public RoleAttribute(Role role)
	{
		CreateRole = role;
	}

	public Role CreateRole
	{
		get { return this.createRole; }
		set { this.createRole = value; }
	}

	private Role createRole = 0;
	public const Role DefaultRole = Role.Administrator
	 | Role.BackupOperator;
}

[RoleAttribute(RoleAttribute.DefaultRole)]
public class DatabaseAccount
{
	//.............. 
}

RoleAttribute, instead of taking an array, would only take a single argument of flags (appropriately or-ed). If the underlying type of the Role enum is long or ulong, that gives you 64 different Roles.

[Author: SantoshZ]

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Matthew W. Jackson said on December 3, 2004 10:08 AM:
In practice, static readonly arrays are pretty useless.

Sure, you cannot reassign the array itself, but you can replace individual values. The "readonly" just makes you think it cannot be changed, which is bad because you end up with mistakes like System.IO.Path.InvalidPathChars.

Since some malicious code may change an InvalidPathChars entry, it is useless for validation.
# JC# said on December 23, 2004 12:46 AM:
PERFECT!
# FAQ C# said on January 5, 2005 5:32 PM:
# FAQ C# (par Yannick Lejeune) said on August 23, 2005 5:14 AM:
# FAQ C# (par Yannick Lejeune) said on August 23, 2005 5:17 AM:
# Igor said on November 4, 2009 9:20 AM:

The following code compiles with no problem:

const int[] constArray = null;

constArray[0] = 1;

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker