Is there a max limit restriction on boolean array size in C#?
Yes, see Array.MaxLength:
This property represents a runtime limitation, the maximum number of elements (not bytes) the runtime will allow in an array. There is no guarantee that an allocation under this length will succeed, but all attempts to allocate a larger array will fail.
This should in most cases be equal to int.MaxValue
, or 2147483647. So a million elements should work just fine unless you are running in a very memory restricted environment.
A more likely reason for your problems is that the number of elements are more than you are expecting. So some operation is giving an error since the array is not large enough to hold all elements.
A common way to store variable size data in a file on disk is to prefix the data with the length, something similar to:
public static bool[] ReadBoolArray(BinaryReader reader){
var length = reader.ReadInt32();
var result = new bool[length];
for(var i = 0; i < length; i++){
result[i] = reader.ReadBoolean();
}
return result;
}
You may have issues if either the file does not include the number of items in the array, you are mistaken about the format the file stores the data in, or if the file is somehow corrupt.