TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Character Literal

This C# article examines character literals, which use single-quotes. It demonstrates escape sequences.

Character literal. A character literal is a value.

It is usually assigned to a variable of type char. Character literals in the C# language are enclosed in single-quote characters. Some values must be escaped with backslash characters.

Example. First we see a program that demonstrates the usage of character literals in the C# language. Char variables, such as those at the class level or the local variable level, are assigned to the character literal values.

Tip: The character literals are immutable and cannot be changed, but the variables that store those values simply denote storage locations.

Next: The const char in the program uses value semantics and cannot be reassigned. It is a named literal.

ConstString Literal

C# program that uses character literals

using System;

class Program
{
    static char _literal5 = 'e'; // Static character literal.
    const char _literal6 = '#'; // Constant character literal.

    static void Main()
    {
	//
	// Shows some example character literals.
	// ... Also shows a local constant character literal.
	// ... Then prints them.
	//
	char literal1 = 'A';
	char literal2 = 'b';
	char literal3 = '\\';
	const char literal4 = '_';

	Console.WriteLine(literal1);
	Console.WriteLine(literal2);
	Console.WriteLine(literal3);
	Console.WriteLine(literal4);
	Console.WriteLine(_literal5);
	Console.WriteLine(_literal6);
    }
}

Output

A
b
\
_
e
#

This program uses character literals. It prints the literal values to the screen with Console.WriteLine. The character literals are enclosed in single-quote marks, and backslash character is itself escaped with a backslash.

Console.WriteLine

Tip: Character literals are escaped with the "\" character, and this is how you can represent the newline "\n".

Environment.NewLine

So: The program shows how variables, such as char variables, relate to values such as the literals themselves and const char value names.

Discussion. A variable, such as a char, denotes a storage location. This can be reassigned to different values. A character literal, such as "A", denotes a value, and this can never be changed in memory. Character literals denote values.

Further: A const char can only be used as a value, not a variable. It is a way of referring to a character literal by a name.

Summary. Character literals represent character values. They are a form of value representation, not a variable type. They cannot be changed during runtime, but the variables that store them can.

Also: We escaped the newline as "\n". Character literals are similar to constant two-byte integers in the language.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf