Hot File

How to C# Explicit Casting

View: 222    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: C / C++ / MFC   Fields: Other

0 point/0 review File has been tested

Explicit casting is an instruction, C# gives that instruction to compiler to convert a data type / expression into a specific type.

Introduction

Explicit casting is an instruction, C# gives that instruction to compiler to convert a data type / expression into a specific type.

In above program an int is assigned to a long data type, byte is assigned to short and float is assigned to double data type, that is done by an implicit casting which is automatically performed by c# compiler because that data types are compatible with each other and destination variables has greater range then the source variables.

Notice, if i assign long data type to an int, short is assigned to byte and double is assigned to float; these will not be converted implicitly or automatically by c# compiler because i am trying to assigning great range data types to short range data types. At this stage a compile time error will be occurred. Fortunately i can still assign great range data type’s values to short range data type variables by using explicit or manual casting.

With explicit casting we can also perform type conversion / casting between incompatible data types such as convert / assign an int data type into a char data type

Explicit casting is performing by a user / developer manually

Some data or value can be lost during perform the explicit casting, because if we assign double data type’s value to an int then the fractional points of double can be lost because we are assigning floating points value to an integer type

Explicit casting is very useful in some situations in C#.

Syntax:

data_type var_name = (target_type) source_type or expression;

Example, explicit or manual casting / type conversion:

using System;
namespace csharpBasic
{
  // Start class definition / declaration.
    class Program
    {
      // Static main method void type declaration.
        static void Main(string[] args)
        {
         // Declare an int data type variable.
            int i;
 
         // Declare a byte data type variable.
            byte b;
 
         // Assign 12000 value to int type i variable.
            i = 12000;
 
         // Assign an int data type to a byte data type (b) by using an explicit casting/conversion.
            b = (byte)i;
 
         // Print byte b value, some value will be lost.
            Console.WriteLine("byte: {0} ", b);
 
         // Declare a long data type variable.
            long l;
 
         // Declare an int data type variable.
            int il;
 
         // Assign 9500000 value to long type variable.
            l = 9500000;
 
         // Assign a long data type to an int data type (il) by using an explicit casting/conversion.
            il = (int)l;
 
         // Print an int il, no value will be lost.
            Console.WriteLine("int: {0} ", il);
 
         // Declare a double data type variable d1.
            double d1;
 
         // Declare a double data type variable d2.
            double d2;
 
         // Declare an int data type variable.
            int di;
 
         // Assign 54.22 value to double data type d1.
            d1 = 54.22;
 
         // Assign 55.2 value to double data type d2.
            d2 = 55.2;
 
         // Assign two double data type to an int data type (di) by using an explicit casting/conversion.
            di = (int)(d1 + d2);
 
         // Print int di, notice fractional points will be lost of double.
            Console.WriteLine("int: {0} ", di);
 
         // Declare an int data type variable.
            int ic;
 
         // Declare a char (character) data type variable.
            char ch;
 
         // Assign 3 value to an int (ic).
            ic = 3;
 
         // Assign an int data type to char data type (ch) by using an explicit casting/conversion.
            ch = (char)ic;
 
         // print char ch.
            Console.WriteLine("char: {0} ", ch);
 
            Console.ReadKey();
 
        } // End of main method definition.
    } // End of class.
}
Related Posts

How to C# Explicit Casting

How to C# Explicit Casting Posted on 07-09-2016  Explicit casting is an instruction, C# gives that instruction to compiler to convert a data type / expression into a specific type. 5/10 222

Comment:

To comment you must be logged in members.

Files with category

  • How to Swap Two Numbers using Call by Reference in C

    View: 370    Download: 0   Comment: 0   Author: none  

    How to Swap Two Numbers using Call by Reference in C

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

  • How to Print Fibonacci Series using Recursion in C, C++

    View: 376    Download: 0   Comment: 0   Author: none  

    How to Print Fibonacci Series using Recursion in C, C++

    Category: C / C++ / MFC
    Fields: Other

    4.5/1 review
    Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

  • How to Implicitly Typed Arrays In C#

    View: 410    Download: 0   Comment: 0   Author: none  

    How to Implicitly Typed Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/1 review
    When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.

  • How to C# Static vs Instance Methods As Event Handlers

    View: 286    Download: 0   Comment: 0   Author: none  

    How to C# Static vs Instance Methods As Event Handlers

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    Both static and instance methods can be used as event handlers in C#

  • Build Pass By Reference To Method In C#

    View: 439    Download: 0   Comment: 0   Author: none  

    Build Pass By Reference To Method In C#

    Category: C / C++ / MFC
    Fields: Other

    0/3 review
    Like value types such as int, double, char etc. We can also pass a reference variable to the method in C# as its parameter which allows us to pass an object to the method and it is called pass by reference to method in C#.

  • How to C# Continue Statement

    View: 273    Download: 0   Comment: 0   Author: none  

    How to C# Continue Statement

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    continue statement skips or exits the current execution of a loop by given condition and keep execution continue for its next iteration unlike break statement it does not exit or terminate from loops, it terminates only its current execution

  • How to Single Dimensional Arrays In C#

    View: 212    Download: 0   Comment: 0   Author: none  

    How to Single Dimensional Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/1 review
    Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an...

  • Build C# Multilevel Inheritance

    View: 236    Download: 0   Comment: 0   Author: none  

    Build C# Multilevel Inheritance

    Category: C / C++ / MFC
    Fields: Other

    2.5/2 review
    C# supports multilevel inheritance, suppose we have four classes A, B, C and D. Class A can inherit to B, B to C and C can inherit to D and so on now class D has all public members of class A, B and C include its own.

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd