Search code examples
c#arraysarray-initialize

C#: PointF() Array Initializer


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?


Solution

  • 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".