9

Is there a way I can create a static array with readonly values, but using some logic to create it? Let me try to explain:

I know I can do this:

public static readonly int[] myArray = { 1, 2, 3 };

but is it possible to do something like:

public static readonly int[] myArray2 = 
{
    for (int i = 0; i < 256; i++)
    {
        float[i] = i;
    }
};

EDITED: A good solution to my question: Static Constructor! http://msdn.microsoft.com/en-us/library/k9x6w0hc%28v=VS.100%29.aspx :D

2
  • Yes static constructor is what I showed you in my post ;-)
    – Arnaud F.
    Sep 15, 2011 at 21:07
  • The question is not properly clear. The English is not my language, but I understand you are going to create an "array with readonly values", thus its cells cannot be modified, not the array instance itself. Sep 18, 2011 at 6:15

6 Answers 6

11

What you can do is:

public class YourClass
{
    public static readonly int[] myArray2 = null;

    static YourClass()
    {
        myArray2 = new int[256];
        for (int i = 0; i < 256; i++)
        {
            myArray2 [i] = i;
        }        
    }
}

Or:

public static readonly int[] myArray2 = Enumerable.Range(0, 255).ToArray();
3
  • 1
    This is technically at run-time, rather than at build-time (as requested). However, this is the way I would do it, since C# is lacking a macro system. Sep 15, 2011 at 14:40
  • I think it's an overkill. If you can size the array inline or in the ctor, its content may be set/changed anytime. Sep 15, 2011 at 14:49
  • I want to create the array only one time, in a static property, no problem if it is at run-time, I expressed myself badly. I think this solution (static class or a singleton) solves my problem. Thanks.
    – Pedro77
    Sep 15, 2011 at 17:32
2

Yes, try it out:

public static readonly IEnumerable<int> myArray = CreateValues();
public static IEnumerable<int> CreateValues()
{
  return new int[] { 1, 2, 3 };
}
2
public static readonly int[] Array = CreateArray();

private static int[] CreateArray()
{
    return new[] { 1, 2, 3, 4, 5 };
}
1

The "readonly" applies to the array instance, not to its content. Thus, you will not able to replace the original array, or modify the size. However, there's no constraint on the array's cells, that might be modified anytime.

2
  • Thanks for remembering me that. Any tip to solve this problem?
    – Pedro77
    Sep 15, 2011 at 19:49
  • Not sure to understand you goal, though. Do you want to seal the array once it has been filled (i.e. none else could modify any of the cells)? None of the proposed solution do that, so why are you asking for a "readonly" field? Sep 16, 2011 at 7:06
1
public static readonly int[] myArray2 = ((Func<int[]>)(() => {
    var array = new int[] { 1, 2, 3 };
    return array;
}))();

MORE unreadable!!! Hasn't the Javascript taught you anything? :-)

(note, this is a Joke! It's only interesting because it shows you the C# compiler can't auto-discover the type of this lambda function)

In Javascript you would write something very similar to this:

// ILLEGAL IN C#!!! DANGER, WILL ROBINSON!
public static readonly int[] myArray2 = (() => {
    var array = new int[] { 1, 2, 3 };
    return array;
})();

Now I'll take a look at the "working" C# way (oh! The horror!)

public static readonly int[] myArray2 = (
    (Func<int[]>) /* <-- The cast to Func<int[]> delegate */ 
    ( 
    /* Start Declaration --> */ 
        () => { var array = new int[] { 1, 2, 3 }; return array;
    } /* <-- End Declaration */ 
    )
    ) 
    (); /* <-- Here we call it! */ 
4
  • C# can discover the type of a delegate (that's what Func<> is). It CAN also discover the type of a lambda (which is probably what you meant). What it cannot do is automatically discover the type of a lambda based solely on the return statement. Sep 15, 2011 at 14:43
  • That being said, this is ugly, compared to Enumerable.Range(0, 256).ToArray() Sep 15, 2011 at 14:44
  • Yeah, sure. I get that. I'm just saying that this would bring maintainers nightmares. Sep 15, 2011 at 14:45
  • @John I think it's more interesting for its comparison to the Javascript notation. The JS notation is VERY common (for example in jQuery). It's like saying: here you can do this, here you can't. I lost an hour the first time (some months ago) I tried to convert it from JS.
    – xanatos
    Sep 15, 2011 at 14:48
0

Actually keyword readonly means that you can assign/modify the variable only into a constructor.

public static readonly int[] values = Enumerable.Range(0, 256).ToArray();

or even

public static readonly IEnumerable<int> values = Enumerable.Range(0, 256).ToArray();

See MSDN.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.