The SLAR on System.Byte
Continuing in the series on sharing some of the information in the .NET Framework Standard Library Annotated Reference Vol 1 here is an annotation from the System.Byte class. Boy, was this ever a controversial point when we first started the framework, but time has so far shown that this is just not a big issue.
BA - The naming of this class diverges from our pattern for naming integral base
types. Strictly following the pattern would have led us to naming this class UInt8.
How ugly that would have been for such a commonly used type! After much debate
we decided that bytes are so commonly mentioned in our APIs (ReadByte, Write-
Byte on Stream, for example) that it warranted having a good name for the base type
even though it meant breaking the naming pattern. Similarly we include Byte rather
than SByte in the Common Language Specification because it is much more common
to view bytes as unsigned 0..255 values than –128..127 values.
And here is a sample, pretty simple, but interesting none the less
using System;
namespace Samples
{
public class ByteParse
{
public static void Main()
{
try
{
Byte b = Byte.Parse(" 0 ");
Console.WriteLine("b is {0}", b);
b = Byte.Parse(" +255 ");
Console.WriteLine("b is {0}", b);
b = Byte.Parse("256");
Console.WriteLine("b is {0}", b);
}
catch(OverflowException e)
{
Console.WriteLine("Exception: {0}", e);
}
}
}
}
And the output:
b is 0
b is 255
Exception: System.OverflowException: Value was either too large or too small for an unsigned byte.
at System.Byte.Parse(String s, NumberStyles style, NumberFormatInfo info)
at System.Byte.Parse(String s)
at Samples.ByteParse.Main() in program.cs:line 15