Skip to main content

How do I create a constant that is an array?


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.

Comments

Popular posts from this blog

Top Open Source Web-Based Project Management Software

This is an user contributed article. Project management software is not just for managing software based project. It can be used for variety of other tasks too. The web-based software must provide tools for planning, organizing and managing resources to achieve project goals and objectives. A web-based project management software can be accessed through an intranet or WAN / LAN using a web browser. You don't have to install any other software on the system. The software can be easy of use with access control features (multi-user). I use project management software for all of our projects (for e.g. building a new cluster farm) for issue / bug-tracking, calender, gantt charts, email notification and much more. Obviously I'm not the only user, the following open source software is used by some of the biggest research organizations and companies world wild. For example, NASA's Jet Propulsion Laboratory uses track software or open source project such as lighttpd / phpbb use re

Google products for your Nokia phone

Stay connected with Gmail, Search, Maps and other Google products. Check products are available for your Nokia phone Featured Free Products Search - Find the information you need quickly and easily Maps - Locate nearby businesses and get driving directions Gmail - Stay connected with Gmail on the go YouTube - Watch videos from anywhere Sync - Synchronize your contacts with Google

My organization went through the approval process of supporting the .NET Framework 2.0 in production. Do we need to go through the same process all...

My organization went through the approval process of supporting the .NET Framework 2.0 in production. Do we need to go through the same process all over again for the .NET Framework 3.0? Do I need to do any application compatibility testing for my .NET Framework 2.0 applications? Because the .NET Framework 3.0 only adds new components to the .NET Framework 2.0 without changing any of the components released in the .NET Framework 2.0, the applications you've built on the .NET Framework 2.0 will not be affected. You don’t need to do any additional testing for your .NET Framework 2.0 applications when you install the .NET Framework 3.0.