Is there anything like a “Call constraint” for C# ?
For instance i have the following function:
public UInt16 ConvertByteToUInt16 (byte[] buffer)
{
if (buffer.Length != 2)
{
throw new InvalidArgumentException();
}
Convert();
}
Is it possible to write something like:
public UInt16 ConvertByteToUInt16 (byte[] buffer) : where (buffer.Lenght = 2)
{
Convert();
}
And if i call the function like that:
ConvertByteToUInt16 (new byte[] { 0xFF, 0xFF, 0xFF } )
I would like to get an error at compile time.
I am quite sure nothing like that exists on C# 2.0, but maybe on C# 4.0 ?
Thanks in advance.
You can’t do this in standard .NET. You need to manually check, then throw an appropriate exception: