Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between char and Char? [duplicate]

Tags:

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?

like image 270
RE6 Avatar asked Aug 18 '12 23:08

RE6


People also ask

What's the difference between char and char?

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.

What's the difference between char * and char []?

The fundamental difference is that in one char* you are assigning it to a pointer, which is a variable. In char[] you are assigning it to an array which is not a variable.

What is the difference between char * array and char array []?

What is the difference between char and char*? char[] is a character array whereas char* is a pointer reference. char[] is a specific section of memory in which we can do things like indexing, whereas char* is the pointer that points to the memory location.


2 Answers

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#

like image 194
Guffa Avatar answered Sep 18 '22 08:09

Guffa


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

like image 29
stakx - no longer contributing Avatar answered Sep 20 '22 08:09

stakx - no longer contributing