1

Possible Duplicate:
What is System.Void?
Why not System.Void?

I noticed that it is not allowed to use typeof(System.Void). But typeof(void) is allowed. When you use typeof(System.String) or typeof(string) there is no difference.

Why is this the case?

Community
  • 1
  • 1
Rookian
  • 19,841
  • 28
  • 110
  • 180

3 Answers3

3

This is because C# defines aliases to keep the original C flavor.

The .NET Framework uses specific naming conventions: for instance it states that a class name should start with a Capital. Therefore, String -- which is not a C# native type but a .NET class defined in the System namespace -- is actually called System.String, or String for short if you have a using System; in your file.

C# defines string as an alias of System.String so that code can look like it does in C and other C-based languages (C++, Java, ...). I think it's more readable. In the same fashion, int is the same as System.Int32.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
1

void is not like string. In almost all cases where void is used in C#, it is given special treatment by the compiler, for example by not requiring a return statement in a void-"returning" method, and by not allowing a return statement with an expression with a "value" of type void. typeof(void) is one of the few cases where void is really treated as a regular type. And it works both ways: the compiler can assume that any type that is not void shouldn't get void's special treatment. Allowing other ways to spell void would make interpreting C# more complicated, and there is no reason why you would ever need it.

0

string is an alias of class System.String.

other list of aliases

  • object: System.Object
  • string: System.String
  • bool: System.Boolean
  • byte: System.Byte
  • sbyte: System.SByte
  • short: System.Int16
  • ushort: System.UInt16
  • int: System.Int32
  • uint: System.UInt32
  • long: System.Int64
  • ulong: System.UInt64
  • float: System.Single
  • double: System.Double
  • decimal: System.Decimal
  • char: System.Char

thanks to Jon Skeet for this.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492