推荐的类型/方式存储null,0

编程入门 行业动态 更新时间:2024-10-25 18:22:31
推荐的类型/方式存储null,0-3 [关闭](Recommended type/way to store null, 0-3 [closed])

我需要存储大量数据。 我需要识别“null”+ 0-3。 基本上我需要区分5个身份。 例如“无”,“向左”,“向上”,“向右”,“向下”。 在C#中存储这个的好方法是什么? 目前我认为它可能是sbyte但我希望一个“更小”的解决方案。

I need to store huge amounts of data. I need to identify "null" + 0-3. Basically i need to differentiate between 5 identities so to say. For example "nothing", "left", "up", "right", "down". What is a good way to store this in C#? At the moment i think it could be sbyte but i hope for a "smaller" solution.

最满意答案

sbyte / byte是C#中的最小单位,除非您创建自己的数据结构,例如使用一个值来存储多个项目,例如使用3位位置来存储一个项目,因此Int16可以存储5个项目。

换句话说,如果不追求空间效率的极端,建议使用枚举来存储有意义的值,并且可以使用byte作为枚举,以便节省空间:

enum Direction : byte {Nothing=0, Left, Up, Right, Down};

这将使代码更清晰。

sbyte / byte is the smallest unit in C# unless you make your own data structure such as use one value to store multiple items, like use 3 bit positions to store one item so Int16 can store 5 items.

In another way, if not pursue the extreme of space efficiency, enum is more recommended to store meaningful values, and you can use byte for an enum so that save the space:

enum Direction : byte {Nothing=0, Left, Up, Right, Down};

That will make the code much clearer.

更多推荐

本文发布于:2023-07-16 15:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1130477.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   方式   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!