Demonstrate casting : Variable Definition « Language Basics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Language Basics » Variable DefinitionScreenshots 
Demonstrate casting
Demonstrate casting

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate casting. 
 
using System; 
 
public 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 = (bytei;  
    Console.WriteLine("b after assigning 255: " + b + 
                      " -- no data lost.")
 
    // cast an int into a byte, data lost 
    i = 257
    b = (bytei;  
    Console.WriteLine("b after assigning 257: " + b + 
                      " -- data lost.")
    Console.WriteLine()
 
    // cast a uint into a short, no data lost 
    u = 32000
    s = (shortu; 
    Console.WriteLine("s after assigning 32000: " + s + 
                      " -- no data lost.");  
 
    // cast a uint into a short, data lost 
    u = 64000
    s = (shortu; 
    Console.WriteLine("s after assigning 64000: " + s + 
                      " -- data lost.");  
    Console.WriteLine()
 
    // cast a long into a uint, no data lost 
    l = 64000
    u = (uintl; 
    Console.WriteLine("u after assigning 64000: " + u + 
                      " -- no data lost.");  
 
    // cast a long into a uint, data lost 
    l = -12
    u = (uintl; 
    Console.WriteLine("u after assigning -12: " + u + 
                      " -- data lost.");  
    Console.WriteLine()
 
    // cast an int into a char 
    b = 88// ASCII code for X 
    ch = (charb; 
    Console.WriteLine("ch after assigning 88: " + ch);  
  }    
}

           
       
Related examples in the same category
1.Declaring a variable.
2.Initializing a variable.
3.Variable default nameVariable default name
4.Heap and Stack Memory
5.Two reference type variables may refer (or point) to the same objectTwo reference type variables may refer (or point)
               to the same object
6.Create a 4-bit type called NybbleCreate a 4-bit type called Nybble
7.Use new with a value typeUse new with a value type
8.Init variableInit variable
9.An attempt to reference an uninitialized variable
10.Illustrates variable scope
11.Demonstrate the use of readonly variablesDemonstrate the use of readonly variables
12.Uninitialized Values
13.Int, float, double, decimalInt, float, double, decimal
14.Demonstrate dynamic initializationDemonstrate dynamic initialization
15.Demonstrate block scopeDemonstrate block scope
16.Demonstrate lifetime of a variableDemonstrate lifetime of a variable
17.This program attempts to declared a variable in an inner scope
18.A promotion surpriseA promotion surprise
19.Using casts in an expressionUsing casts in an expression
20.Create an implication operator in C#Create an implication operator in C#
21.declaring a reference type variable and creating an object the variable will reference
22.Definite Assignment and ArraysDefinite Assignment and Arrays
23.Variable Scoping and Definite Assignment:Definite AssignmentVariable Scoping and Definite Assignment:Definite Assignment
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.