DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Constant string array in C# ?

  1. #1
    K.C. Guest

    Constant string array in C# ?



    I've been trying to create a constant string array in C#.

    public const string[] = new string[] {"X","y","Z"};

    This doesn't work, I've tried other incarnations too.
    Maybe I've just been awake too long.

    Any help ?
    Thank you
    K.C.



  2. #2
    M. Yang Guest

    Re: Constant string array in C# ?


    You missed the identifier but this is not the point. The point is, an array
    is a reference type while a const is a value type that its value never changes.
    Thus, you can declare a const array.



    "K.C." <KC.Mailroom@CarterWorld.com> wrote:
    >
    >
    >I've been trying to create a constant string array in C#.
    >
    >public const string[] = new string[] {"X","y","Z"};
    >
    >This doesn't work, I've tried other incarnations too.
    >Maybe I've just been awake too long.
    >
    >Any help ?
    >Thank you
    >K.C.
    >
    >



  3. #3
    Kunle Odutola Guest

    Re: Constant string array in C# ?


    "K.C." <KC.Mailroom@CarterWorld.com> wrote in message
    news:3c226a00@147.208.176.211...
    >
    >
    > I've been trying to create a constant string array in C#.
    >
    > public const string[] = new string[] {"X","y","Z"};
    >
    > This doesn't work, I've tried other incarnations too.
    > Maybe I've just been awake too long.
    >
    > Any help ?


    Yes.

    You can't create a 'const' array because arrays are objects and can only be
    created at runtime and const entities are resolved at compile time.

    What you can do instead is to declare your array as "readonly". This has the
    same effect as const except the value can be set at runtime. It can only be
    set once and it is thereafter a readonly (i.e. const) value.

    Kunle



  4. #4
    Greg Guest

    Re: Constant string array in C# ?


    "Kunle Odutola" <kunle.odutola@<REMOVETHIS>okocha.freeserve.co.uk> wrote:
    >
    >"K.C." <KC.Mailroom@CarterWorld.com> wrote in message
    >news:3c226a00@147.208.176.211...
    >>
    >>
    >> I've been trying to create a constant string array in C#.
    >>
    >> public const string[] = new string[] {"X","y","Z"};
    >>
    >> This doesn't work, I've tried other incarnations too.
    >> Maybe I've just been awake too long.
    >>
    >> Any help ?

    >
    >Yes.
    >
    >You can't create a 'const' array because arrays are objects and can only

    be
    >created at runtime and const entities are resolved at compile time.
    >
    >What you can do instead is to declare your array as "readonly". This has

    the
    >same effect as const except the value can be set at runtime. It can only

    be
    >set once and it is thereafter a readonly (i.e. const) value.
    >
    >Kunle



    You need to be careful with this method. The array itself can not be modified
    at runtime, but the objects IN the array CAN be.

    if you have :
    \private readonly string[] array = new string[]{"a","b"};\

    then:
    \array = new string[] {"e","f"}; \
    will not even compile (unless the line of code is in the constructor).

    but \array[0] = "e"\ will always work, and produce {"e","b"} from the original
    array.

    -Greg


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center