TheDeveloperBlog.com

Home | Contact Us

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

C# Switch Char, Conditional Character Test

This C# example uses the switch statement on a char variable.

Switch can handle char cases.

Because a char is a value, switch can use jump tables to test chars. You can take a char value and get a full string version of it—using the switch statement on characters.

Char

Example. First, there are several ways of looking up characters and getting equivalent values for them. Some options include if conditional statements, switch statements, and lookup tables.

Note: The example here shows the switch statement used on chars in the C# language.

C# program that switches on char

using System;

class Program
{
    static void Main()
    {
	char input1 = char.Parse("s");
	string value1 = SwitchChar(input1);

	char input2 = char.Parse("c");
	string value2 = SwitchChar(input2);

	Console.WriteLine(value1);
	Console.WriteLine(value2);
	Console.WriteLine(SwitchChar('T'));
    }

    static string SwitchChar(char input)
    {
	switch (input)
	{
	    case 'a':
		{
		    return "Area";
		}
	    case 'b':
		{
		    return "Box";
		}
	    case 'c':
		{
		    return "Cat";
		}
	    case 'S':
	    case 's':
		{
		    return "Spot";
		}
	    case 'T':
	    case 't':
		{
		    return "Test";
		}
	    case 'U':
	    case 'u':
		{
		    return "Under";
		}
	    default:
		{
		    return "Deal";
		}
	}
    }
}

Output

Spot
Cat
Test

This code takes an arbitrary char value, which is returned from the char.Parse method, and passes the char as an argument to the SwitchChar method. The SwitchChar method uses the switch control statement.

Note: It tests if the char is equal to known character value, or if it is not. The default case deals with all other cases.

Uppercase letters. The three cases S, T, and U in the switch statement are stacked on the lowercase equivalents. This is one way to normalize data and treat 's' and 'S' equivalently. Another option is to use char.ToLower.

char.ToLower

Internals. Internally, you will find that char switches result in the jump table instruction, such as "switch (L_002e, L_0034, L_003a)". Jump tables are a way to achieve much faster lookup by using multiplication and addition instructions.

So: The value of the switch value is used to find the correct case. This is faster than using if-statements in common situations.

Switch Enum

Summary. You can switch on the character type in the C# language. This is an efficient and terse way to find the values of individual characters. In the language, you cannot switch on character ranges, but must stack the cases.

Review: The switch statement on char is compiled into an efficient jump table, often providing faster lookup than if-statements.


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