0

I am trying to write a program in which, I want to initialize 4 starting values of an array, while the other 16 will be entered manually from the console.

For this I have written the following code:

int[] intArray = new int[20] { 20, 22, 23, 0 }; 

But I am getting the following exception: "An array initializer of '20' is expected"

I know that it can be done by this syntax :

int[] intArray = new int[20];
intArray[0] = 20;
intArray[1] = 22;
intArray[2] = 23;
intArray[3] = 0;

I would like to know if this can be achieved in one line.

3
  • 4
    "but i wanna know any how can i do this in one line" - why do you need to? Basically there's no particularly simple way of doing this. There are various ways you could do it with LINQ, but nothing desperately simple. new[] { 20, 22, 23, 0 }.Concat(Enumerable.Repeat(0, 16)).ToArray() would do it, for example... Commented Aug 9, 2015 at 19:11
  • I assume by one line you mean one statement? Because you could do int[] intArray = new int[20]; intArray[0] = 20; intArray[1] = 22; intArray[2] = 23; intArray[3] = 0; as one line, but it is the same number of statements... Commented Aug 9, 2015 at 19:13
  • yes its working but will you please enplane me how? its working @ jon Skeet Commented Aug 9, 2015 at 19:16

5 Answers 5

2

If you're going to be needing this frequently, you could create your own extension method to populate the values in an existing array:

int[] intArray = new int[20].Populate(20, 22, 23, 0); 

Sample implementation:

public static class ListExtensions
{
    public static TList Populate<TList, TElement>(this TList list, params TElement[] values)
        where TList : IList<TElement>
    {
        // TODO: argument validation

        for (int i = 0; i < values.Length; i++)
            list[i] = values[i];

        return list;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes its one of the best and correct way, but i am looking for something as suggested by @jon skeet.
1

How about keeping it simple:

public static T[] CreateAndInitArray<T>( int len, T[] initialValues )
{
    var temp = new T[len];
    initialValues.CopyTo( temp, 0 );
    return temp;
}

Use:

int[] intArray = CreateAndInitArray( 20, new int[] { 20, 22, 23, 0 } );

The initialValues argument could be a params argument too, but that would easily be confusing considering the length argument. Personally I like the answer Douglas provided better, looks alot more logical.

1 Comment

yes it's a right way to do the thing i want +1 @chris
0

This works in two lines:

  int[] intArray = new int[20];
  new int[4] { 20, 22, 23, 0 }.CopyTo(intArray , 0);

Or even:

  int[] intArray = new int[20];
  new int[] { 20, 22, 23, 0 }.CopyTo(intArray , 0);

Note that the unnamed array goes out of scope right after the semicolon.. - I don't think it gets any simpler!

Comments

0

I found jon skeet suggestion more useful.

new[] { 20, 22, 23, 0 }.Concat(Enumerable.Repeat(0, 16)).ToArray();

Comments

-1

I think that you could do something like this:

    private static int[] getInput()
    {

        int[] array = { 20, 22, 23, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    1, 1 };

        bool done = false;
        int current = 4;
        while (!done)
        {
            Console.WriteLine("Enter in a number to be added to the array");
            string input = Console.ReadLine();
            array[current] = Convert.ToInt32(input);
            current++;
            if (current==array.Length)
            {
                done = true;
            }
        }
        return array;
    }

Comments

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.