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.
Asked
Active
Viewed 42 times
0
-
I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Oct 11 '14 at 06:28
-
The only smaller option is using a BitArray of length 3. – brz Oct 11 '14 at 06:54
-
@brz Isn't `BitArray` a class though? So, it creates overhead, which is not incurred with a `byte`. Besides, it appears that it's just a collection of booleans. – B.K. Oct 11 '14 at 07:03
-
Alex, could you define "huge amounts of data." – B.K. Oct 11 '14 at 07:08
-
This depends on what you're doing with this data. For example, if you're trying to run an algorithm on it, you may find that anything smaller than an `sbyte` makes too much of an impact on performance. @B.K.: Just because the class exposes values as bools doesn't mean it stores them as bools, nor is the "overhead" of a class necessarily an issue if he is storing, say, 100000 values in one instance. – Dark Falcon Oct 11 '14 at 13:16
-
Yes, i will run algorithms over it. 100.000 values in one instance is a good guess. With maybe 100 instances. I could afford the higher memory consumption of byte if it would be faster then an BitArray. The algorithm will only read, compare and write to the value. No conversions or the like. – Alex Oct 11 '14 at 17:01
-
@Alex So, if it's 100,000 values... even with 1 byte per value, you're still well under a megabyte of memory usage per instance. Unless this is for an embedded system, you should not stress over it much. – B.K. Oct 11 '14 at 17:59
2 Answers
3
I can't find any documentation directly from Microsoft to support this, but I'm fairly certain that there's no standalone type smaller than a byte (or sbyte) that could hold one of 5 different identities.
Here's a past Stack Overflow question whose answers support this: Is there a .NET data-type smaller than a byte?

Community
- 1
- 1

Troy Gizzi
- 2,480
- 1
- 14
- 15
1
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.

slepox
- 792
- 4
- 9
-
Why would he use `Int16` to store five flags when he can use a `byte` to do that? – B.K. Oct 11 '14 at 07:06
-
I meant to use Int16 store 5 items of the data (who has 5 flags). for example, bits 0-2 (which has 8 states that could present 5 flags) for item 1, bits 3-5 for item 2, etc. – slepox Oct 11 '14 at 07:15
-