20

Possible Duplicate:
What is the difference between String and string

When I run:

char c1 = 'a';
Console.WriteLine(c1);

and when I run:

Char c2 = 'a';
Console.WriteLine(c2);

I get exactly the same result, a.

I wanted to know what is the difference between the two forms, and why are there two forms?

Community
  • 1
  • 1
RE6
  • 2,524
  • 3
  • 28
  • 53
  • Such questions can be self-answered. From your point above just go ahead with 'if(c1.GetType() == c2.GetType()){ MessageBox.Show("Same"!); }. Compare the getType()-output of both variables. The system will tell you the answer. ^^ – C4d Sep 25 '14 at 12:20

3 Answers3

12

The result is exactly the same. Both represent the same type, so the resulting executables are completely identical.

The char keyword is an alias in the C# language for the type System.Char in the framework.

You can always use the char keyword. To use Char you need a using System; at the top of the file to include the System namespace (or use System.Char to specify the namespace).


In most situations you can use either a keyword or the framework type, but not everywhere. For example as backing type in an enum, you can only use the keyword:

enum Test : int { } // works

enum Test : Int32 {} // doesn't work

(I use int in the example, as You can't use a char as backing type for an enum.)


Related: Difference between byte vs Byte data types in C#

Community
  • 1
  • 1
Guffa
  • 640,220
  • 96
  • 678
  • 956
  • Why the downvote? If you don't explain what it is that you think is wrong, it can't improve the answer. – Guffa Nov 15 '14 at 16:22
  • These are just aliases in C# ** 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 ** – reza akhlaghi Aug 16 '17 at 05:15
5

As far as I know, C# char type keyword is simply an alias for System.Char, so they refer to the same type.

stakx - no longer contributing
  • 77,057
  • 17
  • 151
  • 248
1

The keyword char is an alias of the System.Char type in C#.

Ry-
  • 199,309
  • 51
  • 404
  • 420
HatSoft
  • 10,683
  • 3
  • 25
  • 43