4

I need to hard code an array of points in my C# program. The C-style initializer did not work.

PointF[] points = new PointF{
    /* what goes here? */
};

How is it done?

Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217

4 Answers4

8

Like this:

PointF[] points = new PointF[]{
    new PointF(0,0), new PointF(1,1)
};

In c# 3.0 you can write it even shorter:

PointF[] points = {
    new PointF(0,0), new PointF(1,1)
};

update Guffa pointed out that I was to short with the var points, it's indeed not possible to "implicitly typed variable with an array initializer".

Davy Landman
  • 15,109
  • 6
  • 49
  • 73
2

You need to instantiate each PointF with new.

Something like

Pointf[] points = { new PointF(0,0), new PointF(1,1), etc...

Syntax may not be 100% here... I'm reaching back to when I last had to do it years ago.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
1
PointF[] points = new PointF[]
{
    new PointF( 1.0f, 1.0f),
    new PointF( 5.0f, 5.0f)
};
Peter McG
  • 18,857
  • 8
  • 45
  • 53
1

For C# 3:

PointF[] points = {
   new PointF(1f, 1f),
   new PointF(2f, 2f)
};

For C# 2 (and 1):

PointF[] points = new PointF[] {
   new PointF(1f, 1f),
   new PointF(2f, 2f)
};
Guffa
  • 687,336
  • 108
  • 737
  • 1,005