0

So I know with normal properties you can give it a default value eg.

public int someMethod { get; } = 1;

So my question is it possible to do the same with an array property?

public int[] someMethod {get; } = ??

Thanks!

2
  • 3
    Yes you can do it with an array, or any other data type. And what you type in is exactly the same as if it was some local variable. Commented Mar 22, 2022 at 2:50
  • @gunr2171 thanks! That helped me. I just wasn't sure how to initialise the array without knowing the length of the array but the empty method worked a treat. Thanks Commented Mar 22, 2022 at 3:01

1 Answer 1

1
public int[] someMethod {get; } = new int[] { 1, 2, 3, ... };
Sign up to request clarification or add additional context in comments.

5 Comments

What if I don't know the length of the array?
You can't initialize an array without knowing the length of it.
@gunr2171 you are implicitly specifying the length of the array when you are initializing the default values. dotnetfiddle.net/GaWNxE
Yeah, you either type a number in the square brackets, or you have a set number of elements in the curly braces. Either way, you have to know the length of the array when you initialize it.
If you don't know the length, then List<T> is a better choice. Create List at compile time

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.