178

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!

jaywon
  • 7,818
  • 10
  • 36
  • 47

8 Answers8

160

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
}
Jodrell
  • 31,518
  • 3
  • 75
  • 114
Guffa
  • 640,220
  • 96
  • 678
  • 956
  • what will be returned if i use GetBytes() – Radha Manohar Jan 09 '20 at 14:26
  • 1
    @RadhaManohar byte[] / Byte[]. Two names for the same thing. Even the MSDN documentation switches between them; check out [Encoding.GetBytes MSDN](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding.getbytes?view=netframework-4.8) (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) – Christopher Berman Jan 15 '20 at 13:42
25

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

Adam Maras
  • 24,360
  • 5
  • 60
  • 89
8

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, ...

Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
6

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.

Brian Rasmussen
  • 109,816
  • 33
  • 208
  • 305
6

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

This is pure syntactic sugar.

Gerrie Schenck
  • 21,512
  • 19
  • 65
  • 95
4

They are generally the same.

Aurril
  • 2,449
  • 2
  • 23
  • 38
1

byte is a built-in data type in C#.
System.Byte is a struct that represent a byte and provides extra methods like Parse and TryParse.

byte is alias of System.Byte struct. Different .NET languages have different aliases based on the semantics of the particular language, but they all map to specific types in the .NET framework.

ata
  • 8,403
  • 6
  • 36
  • 63
0

also when using reflection ,,,

Type t=Type.GetType("System.Byte"); //works

Type t=Type.GetType("System.byte"); //doesn't work, I can see no way to use"byte" directly here without converting it to "Byte"
bstpierre
  • 26,946
  • 14
  • 63
  • 100
Colin
  • 19
  • 3
  • 4
    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. – Kirk Woll Apr 26 '11 at 21:20