Covers topics on the Microsoft Certification Exam for the .NET Framework (Exam 70-536, Microsoft .NET Framework - Application Development Foundation)

Thursday, May 22, 2008

What is the difference between byte and Byte in the .NET Framework?

Have you ever started to type 'by' while writing code in Visual Studio and seen both an upper case and lower case "byte" in the intellisense menu and wondered what the difference was?

Lower case "byte" is a built in type in C#. System.Byte is a class built into the .NET framework that represents a byte. The secret is that this built in type is an alias to the System.Byte class. Different .NET languages have different aliases based on the semantics of the particular language, but they all map to specific object types in the .NET framework. This allows code to be written more "cleanly" and still easily compile to the correct type in the .NET framework.

Let's confirm the statement that the C# built in type 'byte' is really an alias of the System.Byte class.

byte and System.Byte Example

using System;
using System.Collections.Generic;
using System.Text;

namespace ByteDifferenceExample
{
class Program
{
static void Main(string[] args)
{
byte small_b_byte = 1;
Byte large_b_byte = 2;

Console.WriteLine("The value of the little b byte is: " + small_b_byte);
Console.WriteLine("The type of the little b byte is: " + small_b_byte.GetType());

Console.WriteLine("----------------------------");

Console.WriteLine("The value of the big b byte is: " + large_b_byte);
Console.WriteLine("The type of the big b byte is: " + large_b_byte.GetType());

Console.WriteLine("----------------------------");

if (small_b_byte.GetType() == large_b_byte.GetType())
{
Console.WriteLine("Summary: byte and Byte have the same type!");
}
else
{
Console.WriteLine("Summary: byte and Byte have different types");
}
}
}
}

Output of byte and System.Byte Example

The value of the little b byte is: 1
The type of the little b byte is: System.Byte
----------------------------
The value of the big b byte is: 2
The type of the big b byte is: System.Byte
----------------------------
Summary: byte and Byte have the same type!

Additional Resources
Built-In Types Table, C# (Microsoft)

3 comments:

Anonymous said...

Thanks for the info. When are you going to post the code sample?

wizkid said...

It is there now :) Sometimes I write the article before I have a chance to write the code sample :)

Anonymous said...

Finally found you... a friend said you were good enough to help me.

You know where to find me.

Support This Site

LinkShare  Referral  Prg