2

I am looking for the shortest way in terms of writing to declare an array of points. My problem is that I have humongous point data that I want to hardcode as an initialization.

These initializations repeat the 'new Point' multiple times:

Point[] points1 = new[] { new Point { X = 0, Y = 0 }, new Point { X = 20, Y = 120 }, new Point { X = 40, Y = 60 }, }; // kinda long typing

Point[] points2 = { new Point(0, 0), new Point(20, 120), new Point(40, 60) }; // better

Alternatively I could declare the array like so:

int[,] arr = new int[,] { { 0, 0 }, { 20, 120 }, { 40, 60 } }; // so far shortest typing

But how can I cast int[,] to Point[] ? Are there other alternatives (like using lists) ?

2 Answers 2

4

You can change new[] to new Point[]. This way, you can use target-typed new in the array elements:

Point[] points1 = new Point[] { 
    new() { X = 0, Y = 0 }, 
    new() { X = 20, Y = 120 },
    new() { X = 40, Y = 60 }, 
};

If Point has a 2-parameter constructor, this can be even shorter:

Point[] points1 = new Point[] { 
    new(0, 0),
    new(20, 120),
    new(40, 160)
};

If points1 is a local variable, you can make it even shorter by making the variable implicitly typed:

var points1 = new Point[] { 
    new(0, 0),
    new(20, 120),
    new(40, 160)
};

If you want to make this really short in the long run, you can make a (int, int)[], then convert to Point[]:

Point[] points1 = new[] { 
    (0, 0),
    (20, 120),
    (40, 160)
}.Select(x => new Point(x.Item1, x.Item2)).ToArray();

In C# 12+, you can also use a collection expression:

Point[] points1 = [ 
    new(0, 0),
    new(20, 120),
    new(40, 160)
];
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but wouldn't initialization of int[,] or List<List<int>>(); be even shorter, ofcourse somehow cast it afterwards to Point[] ?
@1337Grrr new[,] is shorter since you can even get rid of the word new, but I couldn't find a readable way to convert that to Point[], and I personally just prefer creating the thing you want directly, rather than creating something else first, then converting. You can try converting from a (int, int)[]. See the edit.
Since my point list contains large amount of data I don't have any concerns on its readability, just implementing it with the shortest possible initialization. The additional code in your edit will do the job.
2

Definitely not the shortest way, But this approach has its own advantages. and it's only a one-time effort.

Making it configuration-driven will help in modifying the points. if in future you want to add/delete/modify points. It will help you in testing and also provide different02 points.

second, you can manage different points based on the environment as well.

public class Point
{
    public int X { get; set; }
    public int Y { get; set; }
}

public class Root
{
    public Point[] Points { get; set; }
}

string json = File.ReadAllText("inut.json");
Root obj = JsonConvert.DeserializeObject<Root>(json); //Use NewtonSoft.json library to deserialize the JSON to object.

Sample JSON:

{
    "Points": [{
            "X": 0,
            "Y": 0
        },
        {
            "X": 10,
            "Y": 20
        }
    ]
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.