• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Type Casting in C# .Net

Type casting is a concept of converting from one data type into another data type.

C# supports two types of type casting

a) Implicit type casting

under control of CLR

Lower to higher

b) Explicit type casting

under control of programmer

higher to lower

Observations

Byte to long arrrowimplicit

Int to bytearrrow Explicit

Int to float arrrowimplicit

Float to int arrrowexplicit

 

C # supports 4 types of Explicit type casting technique

C ++ style of type casting

Converting

Boxing and un boxing

Parsing

Working with C++ style of type casting

Ex :-  int I = 10;

Byte b =  (byte)  I;

Syntax

DT1   V1 = value;

DT2 V2 =  (DT2) V1;

 

Example on C++ Style of type casting

Open console application project

Write the following code in main method

Int  I = 150;

Byte Sal =  (byte) I ;

Console . Write Line  (Sal);

Console. Read Line ();

Execute the project (f5)  à 150

Observations

In C++ style of typecasting, there is a possibility for loading the data.

Convert

Convert is pre- defined class, which is the part of F

Working with convert class is called as “Converting”

Class contains of set of methods.

Predefined methods on convert class

String x = 123

Byte b = convert. To Byte (X);

Convert. To Byte (value)

Convert. To char (value)

Convert. To string (value)

Convert. To Boolean (value)

Convert. To Float (value)

*

*

*

*

Convert. To INT 16()…………….. for short

Convert. To INT 32()…………….. for int

Convert. To INT 46()…………….. for long

Example on converting

Open Console application project

Write the following code in main method

 

Class Program

{

Static void main  (string [] args)

{

For (int I = 0;I < = 255; I ++)

{

Char  c = Convert. To char (i);

Console. write (c);

}

Console . Read line ();

}

}

F5

 

Working with Boxing and Un Boxing

 

Screenshot_15

 

 

Boxing is a concept of converting value type in to reference types.

Un Boxing is a concept of converting reference type in to  value types.

Note

Console application are especially designed for character based operating system like DOS

As of now, CLR software is not yet introduced for DOS OS, hence console Applications are not Executable in DOS OS.

Console Application contains only LOGIC . but not GUI

In order to develop GUI , Applications Microsoft introduced “Windows forms application” template.

 

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Type Casting in C# .Net"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.