4

I need to initialize an array of three points. I want to write it like below, but only once for three elements.

Point P = new Point { X = 0, Y = 1 };

Point[] P = new Point[3];// <----  ?

How to write correctly?

1
  • @minitech No. I have a task to complete an array of different values Commented Mar 13, 2013 at 15:07

5 Answers 5

15

Here is the code for creating the array of 3 different points:

Point[] points = new Point[] { new Point { X = 0, Y = 1 }, new Point { X = 2, Y = 1 }, new Point { X = 0, Y = 3 } };
Sign up to request clarification or add additional context in comments.

Comments

6

There’s not really a shorthand for that. For three, just write it three times:

Point initial = new Point { X = 0, Y = 1 };
Point[] P = new Point[3] { initial, initial, initial };

6 Comments

Did you mean "initial, initial, initial" instead of "P, P, P"?
@PieterGeerkens: It took a few seconds :D
3 references to the same to the same object
@CuongLe: Sorry, I assumed System.Drawing.Point, which is a value type. I’ll delete this if that’s not the case.
Just for information, in this sample, array size does not have to be explicitly set (The number 3 can be omitted).
|
6

Example below you can create 10 Point using Enumerable.Range

var points = Enumerable.Range(0, 10)
            .Select(x => new Point {X = 0, Y = 1})
            .ToArray();

5 Comments

Enumerable.Repeat()
Repeat would use the same Point instance, right?
@HonzaBrestan: refer Servy's comment
@HonzaBrestan Point is a value type, so that's not possible, unless the OP created his own custom Point class.
@Servy: You are absolutely right
4

Here is the shortest solution:

Point[] points = Enumerable.Repeat<Point>(new Point(0, 1), 3).ToArray();

2 Comments

It's most certainly not the shortest. If you want to be playing code golf here you can use var instead of specifying the type, omit the generic arguments to Repeat, and remove the unneeded whitespace.
"generates a sequence that contains one repeated value." Same instance for full array.
4

Because you question deals about a static fixed length array of point with static coordinates, no needs to bother with LINQ and loops in this context when array initialization is that simple.

So you can initialize an array this way:

Point[] P = new Point[] 
{ 
    new Point { X = 0, Y = 1 }, 
    new Point { X = 0, Y = 1 }, 
    new Point { X = 0, Y = 1 },
    ...
};

or use duck typing type inference (thanks minitech):

var P = new [] 
{ 
    new Point { X = 0, Y = 1 }, 
    new Point { X = 0, Y = 1 }, 
    new Point { X = 0, Y = 1 },
    ...
};

3 Comments

When it takes more time to write the "simple" solution what are you trying to save?
Again, In the context of the question, initializing three static points using the good old array initialization is much faster than looping or using LINQ. I dont mind for other contexts with lots of points or when the array is dynamic, or when points coordinates are calculated.
A static set is going to be a very tiny bit quicker at runtime, but it will take more developer time to write/read/modify. In virtually all context's the few nanoseconds of runtime speed won't be worth even a minute of dev 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.