Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7588297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:53:18+00:00 2026-05-30T19:53:18+00:00

what I want to do is really basic, I think. To code example shows

  • 0

what I want to do is really basic, I think. To code example shows it clearly.

class MyClass{
     public string[] Bar;
}

MyClass Foo = new MyClass();
Foo.Bar = { "word", "word", "word" };

This code gives me an error in Visual Studio C#. (Only assignment, call, increment, decrement, and new object expressions can be used as a statement)

Is there a better way to provide an array to the class? The array could be const for my part.

How can I provide an (const) array to a class from outside?

I don’t want to use the constructor, because the array should be optionally.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:53:19+00:00Added an answer on May 30, 2026 at 7:53 pm

    You can only initialize arrays using the {} without the new operator as part of a declaration (which in turn has to explicitly specify the array type). This has nothing to do with whether it’s in the same class or not:

    int[] x = { 1, 2, 3 }; // Fine
    x = { 4, 5, 6 }; // Fail
    x = new[] { 7, 8, 9 }; // Implicitly typed array as of C# 3
    x = new int[] { 10, 11, 12 }; // Works with all versions of C#
    

    See sections 12.6 (array initializers), 10.5 (field declarations), 8.5.1 (local variable declarations) and 7.6.10.4 (array creation expressions) of the C# 4 specification for details.

    To answer your comment on Darin’s post: no, there’s no such thing as a “const” array in any sense I can imagine you mean. Even if you make the array variable readonly, like this:

    private static readonly int[] Values = { 1, 2, 3 };
    

    that only makes the variable read-only. Values will always refer to the same array object (which will therefore always have 3 elements) but arrays themselves are always mutable. If you want to build a read-only collection, I’d suggest using ReadOnlyCollection<T>, probably via List.AsReadOnly():

    private static readonly ReadOnlyCollection<int> Values =
        new List<int> { 1, 2, 3 }.AsReadOnly();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really want to write .NET apps that run on any platform (PC, Linux
I really want to get the google Calendar Api up an running. I found
I really want to be able to have a way to take an app
I really want to put in some sort of section handler into App.config that
I really want to know if I'm missing anything obvious in the software I'm
I really want to know your experience at working with ADO.Net datasets (calling stored
I really want to have a column of inifinite length for one of the
I'm a PHP programmer and I really want to increase the quality of my
I've got an NSColor, and I really want the 32-bit RGBA value that it
I need to read the user's fingerprint from my application. What I really want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.