1

I have class like below;

public class PhoneNumbersDto
{            
 public string code { get; set; }
 public int dial { get; set; }
 public string mask { get; set; }
 public string name { get; set; }
}

And I have Values like below;

 {
    "code": "TR",
    "dial": 90,
    "mask": "### ### ## ##",
    "name": "Turkey"
  },
  {
    "code": "TM",
    "dial": 993,
    "mask": "## ######",
    "name": "Turkmenistan"
  },
  {
    "code": "TC",
    "dial": 1649,
    "mask": "### ####",
    "name": "Turks & Caicos Islands"
  }

I want to create a static const array with TheseClassType but i couldn't initilize it.

Is there a way to create static or const array with multiple values at once.

Thank you

2
  • 1
    Why you can't initialize them? Any errors? Any examples? Commented Apr 1, 2020 at 21:44
  • I created an static array but only with one item. But i could'nt create with multiple array at once. I couldn't find soliton maybe i'm bad to search about solution. Commented Apr 1, 2020 at 21:49

1 Answer 1

3
    public static readonly PhoneNumbersDto[] array =
    {
        new PhoneNumbersDto()
        {
            code = "TR",
            dial = 90,
            mask = "### ### ## ##",
            name = "Turkey"
        },
        new PhoneNumbersDto()
        {
            code = "TM",
            dial = 993,
            mask = "## ######",
            name = "Turkmenistan"
        },
        new PhoneNumbersDto()
        {
            code = "TC",
            dial = 1649,
            mask = "### ####",
            name = "Turks & Caicos Islands"
        }
    }; 

As @eocron said, initialization can work; above is an example. Though for better style, I recommend that you use a constructor taking these four inputs, rather than the style shown.

readonly is used in place of const - you can only declare compile-time items as constants. readonly just means you can't edit the reference to this array.

Sign up to request clarification or add additional context in comments.

3 Comments

For completeness, it should be pointed out that the array elements can be changed, even though the array reference itself cannot. If complete immutability is required, ReadOnlyCollection<PhoneNumbersDto> or a similar type should be used (which could be initialized from the expression shown here, except you would need an explicit new).
@madreflection: absolutely right. There are a lot of design improvements that can be made here, including the decision of this being a static field in the first place. Perhaps a method returning a ReadOnlyCollection could be better. Hopefully this is a step in the right direction.
It's more about information (answer) improvement than anything to do with the design. You say that "you can't edit the reference to this array", and you're correct, but a novice might incorrectly assume that the statement extends to array elements. So to prevent that assumption from lasting past that statement, it's good to point out the difference.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.