CSharp.Net-Informations.com

  How to use C# switch case statements




   Categories

    HOME
    VB.NET
    CSHARP


   














How to use C# switch case statements

The C# switch statement allows you to choose from many statements based on multiple selections by passing control to one of the case statements within its body. The switch statement executes the case corresponding to the value of the expression . The switch statement can include any number of case instances.

  switch (expression)
  {
     case expression:
	    //your code here
	    jump-statement
     default:
	    //your code here
	    jump-statement

}

expression : An integral or string type expression.

jump-statement : A jump statement that transfers control out of the case body.

If any of the expression passed to switch case does not match with case statement the control will go to default: statement . If there is no default: statement control will go outside of the switch statement.


         C# Source Code Download           Print Source Code
         How to use C# switch case statements - Download
        
C# Tutorial

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int val = 5;
            switch (val)
            {
                case 1:
                    MessageBox.Show("The day is - Sunday");
                    break;
                case 2:
                    MessageBox.Show("The day is - Monday");
                    break;
                case 3:
                    MessageBox.Show("The day is - Tuesday");
                    break;
                case 4:
                    MessageBox.Show("The day is - wednesday");
                    break;
                case 5:
                    MessageBox.Show("The day is - Thursday");
                    break;
                case 6:
                    MessageBox.Show("The day is - Friday");
                    break;
                case 7:
                    MessageBox.Show("The day is - Saturday");
                    break;
                default:
                    MessageBox.Show("Out of range !!");
                    break;
            }
        }
    }
}
		

CSharp Statements Related Contents
*     How to use C# if else statements
*     How to use C# for loop
*     How to use C# foreach loop
*     How to use C# while loop
*     How to use C# do while loop
*     How to use C# exceptions statements

CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
Search here for more CSharp Source Code :

  |  Home   |  SiteMap   |  About   |
net-informations.com (C) 2010 All Rights Reserved