简体   繁体   English

跳过为结构提供完整名称空间引用的最佳方法是什么?

[英]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.

相关问题 C#数据结构-引用调用对象属性的最佳方法是什么? - C# data structure - what is the best way to reference a calling object's property? 在C#中设计全屏应用程序的最佳方法是什么? - What's the best way to design a full screen application in C#? 在整个命名空间中引用某个变量的最佳方法? - Best way to reference a certain variable throughout the namespace? 在大型 c# 应用程序上进行批量命名空间重命名的最佳方法是什么? - What's the best way to do a bulk namespace rename on a large c# application? 构造SCPI设备类的最佳方法是什么? - What is the best way to structure a SCPI device class? 模拟.net服务引用的最佳方法是什么,例如Amazon的Web服务 - What's the best way to mock a .net service reference, like Amazon's web services 在c#中获取被调用可执行文件的完整路径和文件名的最佳方法是什么? - What's the best way to get the full path and filename of the invoked executable in c#? 构造代码以允许多个开发人员更新构造函数的最佳方法是什么? - What's the best way to structure code to allow a constructor to be updated by multiple developers 什么是多线程的最佳方式? - What's the best way to multithread? 从集合中删除/跳过项目的最佳方法是什么 - What is the best way to remove/skip an item from collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM