how to declare a const byte[]

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
 
J

Jon Skeet [C# MVP]

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.
 
G

Guest

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!
***************************
 
M

Mattias Sjögren

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
 
Joined
Sep 23, 2009
Messages
1
Reaction score
0
Use " static readonly byte[] ByteArray = { 69, 110, 99 }; " instead, I hope it will help :)
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top