Core C#

Multilabel Switch Statements

This C# program demonstrates how to use a multilabel switch statement.

Program.cs

using System;

namespace XoaX {
    class Program {
        static void Main(string[] args) {
            string sHexadecimal = "f82c";
            int iValue = 0;
            foreach (char cCurr in sHexadecimal) {
                iValue *= 16;
                switch (cCurr) {
                    case 'a':
                    case 'A': {
                            iValue += 10;
                            break;
                        }
                    case 'b':
                    case 'B': {
                            iValue += 11;
                            break;
                        }
                    case 'c':
                    case 'C': {
                            iValue += 12;
                            break;
                        }
                    case 'd':
                    case 'D': {
                            iValue += 13;
                            break;
                        }
                    case 'e':
                    case 'E': {
                            iValue += 14;
                            break;
                        }
                    case 'f':
                    case 'F': {
                            iValue += 15;
                            break;
                        }
                    default: {
                            iValue += (int)(cCurr - '0');
                            break;
                        }
                }
            }
            Console.WriteLine(sHexadecimal + " in hexadecimal is " + iValue + " in decimal.");
        }
    }
}
 

Output

f82c in hexadecimal is 63532 in decimal.
Press any key to continue . . .
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.