1

Say I wanted to convert a string into a utf8 byte array, I could do it via

System.Text.Encoding.UTF8.GetBytes(...)

or

System.Text.UnicodeEncoding.UTF8.GetBytes(...)

or

System.Text.UTF8Encoding.UTF8.GetBytes(...)

As far as I can tell there is no difference between them. Are they all referring to the same thing? Why are there so many?

user81993
  • 6,167
  • 6
  • 32
  • 64

1 Answers1

3
  1. Both UnicodeEncoding and UTF8Encoding are subclasses of the Encoding class.
  2. UTF8 is a static property of the Encoding class
  3. You can access a static property/field/method of a class by using the name of one of its subclasses, so that UnicodeEncoding.UTF8 in this case is the same as writing Encoding.UTF8
  4. As an exception to point 3, a subclass can "hide" a property/field/method of a subclass by using the new keyword... BUT this doesn't happen in this case.

For points 3 and 4 see for example this question (it is about a method, but the concept is the same)

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280