C#:PointF() 数组初始值设定项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/625690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:46:34  来源:igfitidea点击:

C#: PointF() Array Initializer

c#arraysarray-initialize

提问by Agnel Kurian

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

我需要在我的 C# 程序中硬编码一组点。C 风格的初始值设定项不起作用。

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

How is it done?

它是如何完成的?

采纳答案by Davy Landman

Like this:

像这样:

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

In c# 3.0 you can write it even shorter:

在 c# 3.0 中,你可以写得更短:

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

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

更新Guffa 指出我要缩短 ,var points确实不可能“使用数组初始值设定项隐式输入变量”。

回答by i_am_jorf

You need to instantiate each PointF with new.

您需要使用 new 实例化每个 PointF。

Something like

就像是

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

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

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

语法在这里可能不是 100% ......我回到几年前我最后一次不得不这样做的时候。

回答by Peter McG

PointF[] points = new PointF[]
{
    new PointF( 1.0f, 1.0f),
    new PointF( 5.0f, 5.0f)
};

回答by Guffa

For C# 3:

对于 C# 3:

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

For C# 2 (and 1):

对于 C# 2(和 1):

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