Difference between byte vs Byte data types in C#

35,950

Solution 1

The byte keyword is an alias for the System.Byte data type.

They represent the same data type, so the resulting code is identical. There are only some differences in usage:

  • You can use byte even if the System namespace is not included. To use Byte you have to have a using System; at the top of the page, or specify the full namespace System.Byte.

  • There are a few situations where C# only allows you to use the keyword, not the framework type, for example:

.

enum Fruits : byte // this works
{
  Apple, Orange
}

enum Fruits : Byte // this doesn't work
{
  Apple, Orange
}

For detailed other alias, please follow the link.

Solution 2

byte and System.Byte in C# are identical. byte is simply syntactic sugar, and is recommended by StyleCop (for style guidelines).

Solution 3

No difference. byte is alias to System.Byte, the same way int is alias to System.Int32, long to System.Int64, string to System.String, ...

Solution 4

C# has a number of aliases for the .NET types. byte is an alias for Byte just as string is an alias for String and int is an alias for Int32. I.e. byte and Byte are the same actual type.

Solution 5

Nothing, the lowercase one is a keyword which is an alias for the Byte type.

This is pure syntactic sugar.

Share:
35,950

Related videos on Youtube

jaywon
Author by

jaywon

I like code!

Updated on April 21, 2022

Comments

  • jaywon
    jaywon about 2 years

    I noticed that in C# there are both a byte and Byte data type. They both say they are of type struct System.Byte and represent an 8-digit unsigned integer.

    So I am curious as to what the difference if any is between the two, and why you would use one over the other.

    Thanks!

  • Kirk Woll
    Kirk Woll about 13 years
    There is no "converting it to Byte" concept. byte and System.Byte are 100% identical. There is no difference whatsoever. This is unlike Java where they are actually discrete classes.
  • Christopher Berman
    Christopher Berman over 4 years
    @RadhaManohar byte[] / Byte[]. Two names for the same thing. Even the MSDN documentation switches between them; check out Encoding.GetBytes MSDN (which, at the time of this comment, has byte[] as the return type in the method signature, and Byte[] as the return type in the documentation)