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?

Mixer
  • 1,292
  • 3
  • 22
  • 41

5 Answers5

14

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 } };
Marek Musielak
  • 26,832
  • 8
  • 72
  • 80
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 };
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Did you mean "initial, initial, initial" instead of "P, P, P"? – Pieter Geerkens Mar 13 '13 at 14:29
  • @PieterGeerkens: It took a few seconds :D – Ry- Mar 13 '13 at 14:30
  • 3 references to the same to the same object – cuongle Mar 13 '13 at 14:32
  • 1
    @CuongLe: Sorry, I assumed `System.Drawing.Point`, which is a value type. I’ll delete this if that’s not the case. – Ry- Mar 13 '13 at 14:33
  • Just for information, in this sample, array size does not have to be explicitly set (The number 3 can be omitted). – Jaanus Varus Mar 13 '13 at 14:33
  • @minitech congratulations on your run for moderator, you showed toughness and determination, and you held your ground on the q&a. i'm sure of 2 things: you will soon achieve your goal of moderating this community (if you still wish it) AND you will one day agree with me regarding maturity. Oh yeah, did i say that this answer works? UPVOTED! – tony gil Mar 13 '13 at 17:23
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();
cuongle
  • 74,024
  • 28
  • 151
  • 206
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 },
    ...
};
Community
  • 1
  • 1
Larry
  • 17,605
  • 9
  • 77
  • 106
  • When it takes more time to write the "simple" solution what are you trying to save? – Servy Mar 13 '13 at 14:42
  • 1
    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. – Larry Mar 13 '13 at 14:48
  • 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. – Servy Mar 13 '13 at 14:50
4

Here is the shortest solution:

Point[] points = Enumerable.Repeat<Point>(new Point(0, 1), 3).ToArray();
platon
  • 5,310
  • 1
  • 22
  • 24
  • 3
    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. – Servy Mar 13 '13 at 14:35
  • "generates a sequence that contains one **repeated** value." Same instance for full array. – constructor Aug 06 '14 at 14:33