You are on page 1of 3

C3

// Demonstrate casting.

using System;

class CastDemo ,
public static void Main() ,
double x, y;
byte b;
int i;
char ch;
uint u;
short s;
long l;

x = 10.0;
y = 3.0;
// cast an int into a double
i = (int) (x / y); // cast double to int, fractional component lost
Console.WriteLine("Integer outcome of x / y: " + i);
Console.WriteLine();

// cast an int into a byte, no data lost
i = 255;
b = (byte) i;
Console.WriteLine("b after assigning 255: " + b +
" -- no data lost.");

// cast an int into a byte, data lost
i = 257;
b = (byte) i;
Console.WriteLine("b after assigning 257: " + b +
" -- data lost.");
Console.WriteLine();

// cast a uint into a short, no data lost
u = 32000;
s = (short) u;
Console.WriteLine("s after assigning 32000: " + s +
" -- no data lost.");

// cast a uint into a short, data lost
u = 64000;
s = (short) u;
Console.WriteLine("s after assigning 64000: " + s +
" -- data lost.");
Console.WriteLine();

// cast a long into a uint, no data lost
l = 64000;
u = (uint) l;
Console.WriteLine("u after assigning 64000: " + u +
" -- no data lost.");

// cast a long into a uint, data lost
l = -12;
u = (uint) l;
Console.WriteLine("u after assigning -12: " + u +
" -- data lost.");
Console.WriteLine();

// cast an int into a char
b = 88; // ASCII code for X
ch = (char) b;
Console.WriteLine("ch after assigning 88: " + ch);
,
,


1ype converslon
// Demonstrate automatic conversion from long to double.

using System;

class LtoD ,
public static void Main() ,
long L;
double D;

L = 100123285L;
D = L;

Console.WriteLine("L and D: " + L + " " + D);
,
,

C1
public class Program
,
static int Main()
,
MessageBox.Show("Welcome to the Wonderful World of Visual C#",
"Visual C# Tutorials");
return 0;
,
,


C4
forms of the 1 statement: IfSelection.cs
using System;
class IfElseDemo
,
public static void Main()
,
Console.WriteLine("Please enter your name");
string Name = Console.ReadLine();

if (Name == String.Empty)
,
Console.WriteLine("You have not entered your name");
,
else
,
Console.WriteLine("Welcome " + Name);
,
,
,

Switch Statements: SwitchSelection.cs
using System;
class SwitchCase
,
public static void Main()
,
Console.WriteLine("Enter any number between 1 and 30");
int Number = Convert.ToInt16(Console.ReadLine());

switch (Number)
,
case 10:
Console.WriteLine("Entered Number is 10");
break;
case 20:
Console.WriteLine("Entered Number is 20");
break;
case 30:
Console.WriteLine("Entered Number is 20");
break;
default:
Console.WriteLine("Entered Number is not 10,20 or 30");
break;
,
,
,

You might also like