[英]What's the best way to skip giving the full namespace reference to a structure?
I want to declare an array of colors, to be used as 8-bit style sprites. 我想声明一个颜色数组,以用作8位样式精灵。
spriteArray = new System.Drawing.Color[2,2] { {System.Drawing.Color.Red, System.Drawing.Color.Blue}, {System.Drawing.Color.Blue, System.Drawing.Color.Red} };
It's a pain, and looks verbose writing out the namespace path each time - what would the best way to reduce it to something like: 这很痛苦,每次都写出命名空间路径看起来很冗长-将其简化为以下内容的最佳方法是:
spriteArray = new System.Drawing.Color[2,2] {{RED, BLUE}, {BLUE, RED}};
be? 是?
Include a using
directive at the top of your code file: 在代码文件的顶部包括using
指令:
using System.Drawing;
Then it's just: 然后就是:
spriteArray = new Color[2,2]
{
{ Color.Red, Color.Blue },
{ Color.Blue, Color.Red }
};
declare it on top ? 首先声明它?
using System.Drawing;
...
spriteArray = new Color[2,2]
{{Color.Red, Color.Blue},
{Color.Blue, Color.Red}};
or if there is a name conflict, if there is another class with Color
name, you can: 或者,如果存在名称冲突,或者还有另一个具有Color
名称的类,则可以:
using SysDraw = System.Drawing;
...
spriteArray = new SysDraw.Color[2,2]
{{SysDraw.Color.Red, SysDraw.Color.Blue},
{SysDraw.Color.Blue, SysDraw.Color.Red}};
The only way to remove the names completely is to add them as local fields, variables or constants. 完全删除名称的唯一方法是将它们添加为局部字段,变量或常量。 Since Color
is not an enum
, you can't use the const
approach, but you could do: 由于Color
不是enum
,因此不能使用const
方法,但可以这样做:
using System.Drawing;
//...
private static readonly Color Red = Color.Red, Blue = Color.Blue;
//...
spriteArray = new[,] {
{ Red, Blue },
{ Blue, Red },
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.