Setting a Char Value

Posted Mon, Feb 22 2010 13:12 by Deborah Kurata

Strings are a .NET data type that we use every day. But char, not so much.

char is a built-in .Net data type used to declare a Unicode character. Unicode characters are 16-bit and can represent any character used in any known written language.

A char value is defined using a character literal, decimal value, hexadecimal value, or Unicode.

In C#:

// Define a D
// Character literal
char myD  = 'D';
// Decimal
char myD2 = (char)68;
// Hexadecimal
char myD3 = '\x0044';
// Unicode
char myD4 = '\u0044';

Console.WriteLine(myD);
Console.WriteLine(myD2);
Console.WriteLine(myD3);
Console.WriteLine(myD4);

Notice the single quotes in the first example. Using single quotes in C# defines a character literal. Using double-quotes defines a string literal.

In VB:

' Define a D
' Character literal
Dim myD As Char = "D"c
' Decimal
Dim myD2 As Char = ChrW(68)
' Hexadecimal
Dim myD3 As Char = ChrW(&H44)
' Unicode
Dim myD4 As Char = ChrW(&H44)

Console.WriteLine(myD)
Console.WriteLine(myD2)
Console.WriteLine(myD3)
Console.WriteLine(myD4)

Notice the small “c” after the string. Using the “c” in VB defines a character literal.

The above code example results in displaying “D” four times.

Use this technique any time you need to use char data in your application.

Enjoy!

Filed under: , , ,

Comments

# Setting a Char Value to a Symbol

Monday, February 22, 2010 3:15 PM by Deborah's Developer MindScape

This prior post demonstrated how to set char values in VB and C#. But what if you want to display a special

# Linked - List

Tuesday, February 23, 2010 1:35 PM by ASeventhSign

Linked - List

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: