how to declare a const byte[]

  • Thread starter Thread starter Romain TAILLANDIER
  • Start date Start date
R

Romain TAILLANDIER

hi group !

How can i declare a const byte[] in C# ?

const byte[] ByteArray = new byte[] { 69, 110, 99}; // Error : the
expression must be constante
const byte[] ByteArray = { 69, 110, 99}; // Error , the table initialisators
can be used only to initialize variables, try the operator new


i try a few others, but i allreadey have erro like those.
so solve the probleme by using a read only property.
but how could i declare a const byte array ?

thanks
ROM
 
Romain TAILLANDIER said:
How can i declare a const byte[] in C# ?

const byte[] ByteArray = new byte[] { 69, 110, 99}; // Error : the
expression must be constante
const byte[] ByteArray = { 69, 110, 99}; // Error , the table initialisators
can be used only to initialize variables, try the operator new


i try a few others, but i allreadey have erro like those.
so solve the probleme by using a read only property.
but how could i declare a const byte array ?

You can't - consts are for compile-time constant values. An array is a
reference type, and the value itself therefore can't be a constant,
even though the contents are.

You can make the variable readonly, although that won't stop anyone
from changing the contents of the byte array.
 
Constants are value types, so you cannot do this directly. There are ways to
repeat similar funcationality, however, without making constants. Without
understanding what, and more important why, you are doing something, I cannot
give you a great solution here.

---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Constants are value types, so you cannot do this directly.

Not necessarily. You can declare constants of reference types too,
although the values are restricted to string literals and null.



Mattias
 
Use " static readonly byte[] ByteArray = { 69, 110, 99 }; " instead, I hope it will help :)
 
Last edited:
Back
Top