0

I have a list of names which is static. I want to save it in a an static jaggedArray (to avoid multiple copies of same data).

I want to write something like below code

public static string[][] nameList = new string[10][] [{"name1_1","name1_2"},{"name2_1","name2_2"},{"name3_1","name3_2"}....]; 

but C# is not allowing above code. My current code is written as (given below), but for each call it is creatinga a new object

 public static string[][] GetNameList() { string[][] nameList = new string[10][]; nameList [0] = new string[] { "name1_1", "name1_2" }; nameList [1] = new string[] { "name2_1", "name2_2" }; nameList [2] = new string[] { "name3_1", "name3_2" }; ............ return nameList; } 

In above method, the returned object is not static. Can you please help me to create static object. Thanks in advance

Edit : finally got the solution or you can say got the correct syntax ....

public static readonly string[,] UserList = new string[10, 2] { { "UsersId0_0", "UsersId0_1" }, { "UsersId1_0", "UsersId1_1" }.... }; 
3
  • Note that you should try to make static data immutable, i.e. you should probably use static readonly IReadOnlyList<IReadOnlyList<string>> as the signature. Otherwise someone might either replace the entire array, or elements inside the array. Commented Dec 9, 2024 at 14:55
  • thank you @JonasH for your suggestion. I have added this in my code. Commented Dec 9, 2024 at 16:09
  • 1
    if there's always two items, that looks more like a multi-dimensional array. But even then, this sounds like a job for a single dimension array with a tuple, class, struct, or record. Commented Dec 9, 2024 at 17:10

2 Answers 2

0

You can put arbitrary static initialization code in a static constructor:

class MyClass { static MyClass() { // initialize your static array here } } 
Sign up to request clarification or add additional context in comments.

1 Comment

thank you @Jonas Høgh. Its useful comment for me
0

You are mixing the array initializer syntax with collection expressions. new[] { .. } is allowed but new[] [ .. ] not.

However, you can only initialize the first level of a jagged array with an array initializer. The collection initializer however works:

// Collection initializer static string[][] nameList = [ ["name1_1", "name1_2"], ["name2_1", "name2_2"], ["name3_1", "name3_2"] ]; 

The opposite is true for multi-dimensional arrays: array initializers work, collection expressions do not (as of C# 13.0) 😢.

See: https://sharplab.io/#gist:5bb91ae481d9e8c6b18c1cd268b881e6

Note that collection expressions do not require nor allow the new keyword. Array initializer (and collection initializers) are tied to variable declarations, whereas collection expressions can be used anywhere.

Collection expressions ([ ... ]) are a newer syntax and are preferred over array and collection initializers ({ ... }) since they are more versatile and simpler. They also unify the syntax for all kinds of collections and are implemented with a lot of optimizations.

Example of syntax unification:

// Initializer syntax (new must be followed by `{ ... }`, not `[ ... ]`. int[] a1 = new[] { ... }; List<int> l1 = new List<int> { ... }; // Collection expression int[] a2 = [ ... ]; List<int> l2 = [ ... ]; 

You might be tempted to initialize an empty array with

int[] a = new int[0]; 

This allocates new memory every time. It is more optimal to use a predefined (static) empty array:

int[] a = Array<int>.Empty(); 

The following collection expression does this automatically:

int[] a = []; 

2 Comments

Both the solutions (Array initializer and Collection expression) are not working and giving error (Error : Array initializers can only be used in a variable or field initializer. Try using a new expression instead.)
Okay I reworked my whole answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.