If your aim was to convert to an int
, you just need to do that instead:
byte[] numArray1 = new byte[checked((int)response.ContentLength)];
That will avoid you trying to allocate more than 2GB for numArray1
. You don't need to do this from a compiler perspective though - you can allocate an array using a long
value for the length. It's just that it will fall over at execution time if the size is large enough. (In .NET 4.5 you can use an application configuration setting to allow for arrays with a total size of more than 2GB, but I'm not sure offhand whether that allows for the number of elements to exceed int.MaxValue
.)
So actually, you'd be better off with just leaving the checking to the CLR:
byte[] numArray1 = new byte[response.ContentLength];
... or if you do want to impose some maximum size, do that explicitly. (I'd suggest a maximum of less than 2GB, too...)
You should also consider the possibility of ContentLength
being -1, indicating that the client hasn't specified it in a response header.