c# - How to declare a const field of a generic value type provided as generic argument -


i'm trying define generic class accept argument value type (actually enum) , initialize const field it's default type.

i want like:

public abstract class genericclass<valuetype>      valuetype: struct, iconvertible {   public const valuetype val = default(valuetype); } 

unfortunately compiler complaints (i'm using mono think it's same on .net). error following:

error cs1959: type parameter `valuetype' cannot declared const

what's error?

type parameter not allowed constant type.

because struct cannot made const (from c# specification 10.4 constants)

the type specified in constant declaration must sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, enum-type, or reference-type.

a kind of workaround limitation declare static readonly.

public static readonly valuetype val = default(valuetype); 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -