SlideShare a Scribd company logo
C# Tutorial
Part 19:Typecasting
www.siri-kt.blogspot.com
• we can convert one datatype into different datatypes.
• it is classified into 2 types
• 1.implicit type casting
• 2.explicit type casting
• 1.implicit type casting:
• In this we convert lower datatypes are converts into higher data types.it is also called
as internal type casting. It is under control of clr;
• using System;
• namespace implicittypecastin_exm
• { class Program
• { static void Main(string[] args)
• { int i = 545454;//4 bytes
• long l = i;//8 bytes
• Console.WriteLine("the long value is:" + l.ToString());
• Console.ReadLine();
• } } }
• 2.Explicit type casting:
• In this we convert higher datatypes converts into lower datatypes. it is also called as
external type casting. this is under control of the programmer
• using System;
• namespace implicittypecastin_exm
• { class Program
• { static void Main(string[] args)
• { long l = 454545454;//8 bytes
• int i = l;//4 bytes
• Console.WriteLine("the int value is:" + i.ToString());
• Console.ReadLine();
• } } }
• the above example gives error. cant implicit convert higher to lower datatypes. we can
overcome this problem.explit type casting classified into
• 3 types :
• 1.c++ style type casting
• 2.parsing
• 3.converting
• 1.c++ style type casting:
• in this type casting higher datatypes are converts into lower datatypes.
• ex:
• long l = 454545454;//8 bytes
• int i =(int) l;//4 bytes
• Console.WriteLine("the int value is:" + i.ToString());
• Console.ReadLine();
• 2.working with parsing:
• as .net all the datatypes are predefined structures.
• int i;
• int-structure
• i-structure variable.
C# Type Conversion Methods
C# provides the following built-in type conversion methods:
Methods & Description
ToBoolean Converts a type to a Boolean value, where possible.
ToByte Converts a type to a byte.
ToChar Converts a type to a single Unicode character, where possible.
ToDateTime Converts a type (integer or string type) to date-time structures.
ToDecimal Converts a floating point or integer type to a decimal type.
ToDouble Converts a type to a double type.
ToInt16 Converts a type to a 16-bit integer.
ToInt32 Converts a type to a 32-bit integer.
ToInt64 Converts a type to a 64-bit integer.
ToSbyte Converts a type to a signed byte type.
ToSingle Converts a type to a small floating point number.
ToString Converts a type to a string.
ToType Converts a type to a specified type.
ToUInt16 Converts a type to an unsigned int type.
ToUInt32 Converts a type to an unsigned long type.
ToUInt64 Converts a type to an unsigned big integer.
• structure is a collection of predefined methods
• 1.minvalue()
• 2.maxvalue()
• 3.tostring()
• 4.parse()
• working with parse() is called as parsing. parse() method can be used only to convert
from strings into int. tostring() can be used only to convert from int to string.
• ex: print the limits of datatypes.
• static void Main(string[] args)
• { Console.WriteLine(byte.MinValue.ToString());
• Console.WriteLine(byte.MaxValue.ToString());
• Console.WriteLine(int.MinValue.ToString());
• Console.WriteLine(int.MaxValue.ToString());
• Console.ReadLine(); }
• 2.example on parsing
• 1.take the form
• 2.add 3 labels,3 textboxes,1 button control
• private void button1_Click(object sender, EventArgs e)
• { textBox3.Text = textBox1.Text + textBox2.Text; }
• o/p:1010
• it gives concatenation error
• ex:
• "10"+"10"=1010
• 10+10=20;
• we can overcome this problem using parsing method..
• textBox3.Text = (int.Parse(textBox1.Text)
+int.Parse(textBox2.Text)).ToString();
• 2 nd method:
• private void button1_Click(object sender, EventArgs e)
• { // textBox3.Text =
(int.Parse(textBox1.Text)
+int.Parse(textBox2.Text)).ToString();
• int a, b, c;
• a = int.Parse(textBox1.Text);
• b = int.Parse(textBox2.Text);
• c = a + b;
• textBox3.Text = c.ToString();
• }
• Working with converting method:
• working with convert class is called as converting.
• convert is the part fcl(frame work class library)
• convert is the collection of predefined methods
• convert.toint16()-short-2 bytes
• convert.toint32()-int-4 bytes
• convert.toint64()-long-8 bytes
• convert.tochar()
• convert.tobyte()
• convert.toboolean()
• convert.tofloat()
• convert.todouble()
• Example on converting:
• take the form
• 2.add 3 labels,3 textboxes,1 button control
• private void button1_Click(object sender, EventArgs e)
• { textBox3.Text = textBox1.Text + textBox2.Text; }
• o/p:1010
• it gives concatenation error
• ex:"10"+"10"=1010
• 10+10=20;
• we can overcome this problem using parsing method..
• textBox3.Text = (convert.toint32(textBox1.Text)
+convert.toint32(textBox2.Text)).ToString();
• 2 nd method:
• private void button1_Click(object sender, EventArgs e)
• {
• // textBox3.Text = (int.Parse(textBox1.Text)
+int.Parse(textBox2.Text)).ToString();
• int a, b, c;
• a = convert.toint16(textBox1.Text);
• b = convert.toint16(textBox2.Text);
• c = a + b;
• textBox3.Text = c.ToString();
• }
For more visit our website www.siri-kt.blogspot.com
Thanks for
Watching
More Angular JS TutorialsMore C sharp (c#) tutorials

More Related Content

What's hot (14)

PDF
Intake 37 6
Mahmoud Ouf
 
PPTX
Learning python
Hoang Nguyen
 
PDF
Intake 37 5
Mahmoud Ouf
 
PDF
MD-5 : Algorithm
Sahil Kureel
 
PPT
C Omega
iradarji
 
PPTX
30csharp
Sireesh K
 
PPTX
Object oriented programming 9 expressions and type casting in c++
Vaibhav Khanna
 
PPTX
Computer data type and Terminologies
glyvive
 
PDF
SHA 1 Algorithm
Shiva RamDam
 
PPTX
5variables in c#
Sireesh K
 
PPTX
SHA- Secure hashing algorithm
Ruchi Maurya
 
PDF
Aes128 bit project_report
Nikhil Gupta
 
PPT
Stream Based Input Output
Bharat17485
 
Intake 37 6
Mahmoud Ouf
 
Learning python
Hoang Nguyen
 
Intake 37 5
Mahmoud Ouf
 
MD-5 : Algorithm
Sahil Kureel
 
C Omega
iradarji
 
30csharp
Sireesh K
 
Object oriented programming 9 expressions and type casting in c++
Vaibhav Khanna
 
Computer data type and Terminologies
glyvive
 
SHA 1 Algorithm
Shiva RamDam
 
5variables in c#
Sireesh K
 
SHA- Secure hashing algorithm
Ruchi Maurya
 
Aes128 bit project_report
Nikhil Gupta
 
Stream Based Input Output
Bharat17485
 

Similar to 19csharp (20)

PPTX
Ifi7184.DT lesson 2
Sónia
 
PPTX
Data Type in C Programming
Qazi Shahzad Ali
 
PPT
Introduction to VB.Net By William Lacktano.ppt
DonWilliam5
 
PPTX
Programming using c++ tool
Abdullah Jan
 
PPTX
What is Data Types and Functions?
AnuragSrivastava272
 
PPTX
PYTHON TKINTER PPT12TH COMPETITION PYTHOP.pptx
GSwathi21
 
PPTX
Chap_________________1_Introduction.pptx
Ronaldo Aditya
 
PPTX
BASICS OF PYTHON usefull for the student who would like to learn on their own
Nandini485510
 
PPTX
Lecture 2
marvellous2
 
PPT
programming week 2.ppt
FatimaZafar68
 
PPTX
Functional verification using System verilog introduction
JuhaMichel
 
PPTX
Getting started cpp full
Võ Hòa
 
PPT
Basics of c++ Programming Language
Ahmad Idrees
 
PPTX
Learning python
Luis Goldster
 
PPTX
Learning python
Harry Potter
 
PPTX
Learning python
Fraboni Ec
 
PPTX
Learning python
Tony Nguyen
 
PPTX
Learning python
Young Alista
 
PPTX
Learning python
James Wong
 
PPTX
CS4443 - Modern Programming Language - I Lecture (2)
Dilawar Khan
 
Ifi7184.DT lesson 2
Sónia
 
Data Type in C Programming
Qazi Shahzad Ali
 
Introduction to VB.Net By William Lacktano.ppt
DonWilliam5
 
Programming using c++ tool
Abdullah Jan
 
What is Data Types and Functions?
AnuragSrivastava272
 
PYTHON TKINTER PPT12TH COMPETITION PYTHOP.pptx
GSwathi21
 
Chap_________________1_Introduction.pptx
Ronaldo Aditya
 
BASICS OF PYTHON usefull for the student who would like to learn on their own
Nandini485510
 
Lecture 2
marvellous2
 
programming week 2.ppt
FatimaZafar68
 
Functional verification using System verilog introduction
JuhaMichel
 
Getting started cpp full
Võ Hòa
 
Basics of c++ Programming Language
Ahmad Idrees
 
Learning python
Luis Goldster
 
Learning python
Harry Potter
 
Learning python
Fraboni Ec
 
Learning python
Tony Nguyen
 
Learning python
Young Alista
 
Learning python
James Wong
 
CS4443 - Modern Programming Language - I Lecture (2)
Dilawar Khan
 
Ad

More from Sireesh K (20)

PPTX
Cn10
Sireesh K
 
PPTX
chanakya neeti
Sireesh K
 
PPTX
chanakya neeti
Sireesh K
 
DOCX
What is mvc
Sireesh K
 
PPTX
31c
Sireesh K
 
PPTX
31cs
Sireesh K
 
PPTX
45c
Sireesh K
 
PPTX
44c
Sireesh K
 
PPTX
43c
Sireesh K
 
PPTX
42c
Sireesh K
 
PPTX
41c
Sireesh K
 
PPTX
40c
Sireesh K
 
PPTX
39c
Sireesh K
 
PPTX
38c
Sireesh K
 
PPTX
37c
Sireesh K
 
PPTX
35c
Sireesh K
 
PPTX
34c
Sireesh K
 
PPTX
33c
Sireesh K
 
PPTX
30c
Sireesh K
 
PPTX
29c
Sireesh K
 
Cn10
Sireesh K
 
chanakya neeti
Sireesh K
 
chanakya neeti
Sireesh K
 
What is mvc
Sireesh K
 
31cs
Sireesh K
 
Ad

Recently uploaded (20)

PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Horarios de distribución de agua en julio
pegazohn1978
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 

19csharp

  • 2. • we can convert one datatype into different datatypes. • it is classified into 2 types • 1.implicit type casting • 2.explicit type casting • 1.implicit type casting: • In this we convert lower datatypes are converts into higher data types.it is also called as internal type casting. It is under control of clr; • using System; • namespace implicittypecastin_exm • { class Program • { static void Main(string[] args) • { int i = 545454;//4 bytes • long l = i;//8 bytes • Console.WriteLine("the long value is:" + l.ToString()); • Console.ReadLine(); • } } }
  • 3. • 2.Explicit type casting: • In this we convert higher datatypes converts into lower datatypes. it is also called as external type casting. this is under control of the programmer • using System; • namespace implicittypecastin_exm • { class Program • { static void Main(string[] args) • { long l = 454545454;//8 bytes • int i = l;//4 bytes • Console.WriteLine("the int value is:" + i.ToString()); • Console.ReadLine(); • } } } • the above example gives error. cant implicit convert higher to lower datatypes. we can overcome this problem.explit type casting classified into
  • 4. • 3 types : • 1.c++ style type casting • 2.parsing • 3.converting • 1.c++ style type casting: • in this type casting higher datatypes are converts into lower datatypes. • ex: • long l = 454545454;//8 bytes • int i =(int) l;//4 bytes • Console.WriteLine("the int value is:" + i.ToString()); • Console.ReadLine(); • 2.working with parsing: • as .net all the datatypes are predefined structures. • int i; • int-structure • i-structure variable.
  • 5. C# Type Conversion Methods C# provides the following built-in type conversion methods: Methods & Description ToBoolean Converts a type to a Boolean value, where possible. ToByte Converts a type to a byte. ToChar Converts a type to a single Unicode character, where possible. ToDateTime Converts a type (integer or string type) to date-time structures. ToDecimal Converts a floating point or integer type to a decimal type. ToDouble Converts a type to a double type. ToInt16 Converts a type to a 16-bit integer. ToInt32 Converts a type to a 32-bit integer.
  • 6. ToInt64 Converts a type to a 64-bit integer. ToSbyte Converts a type to a signed byte type. ToSingle Converts a type to a small floating point number. ToString Converts a type to a string. ToType Converts a type to a specified type. ToUInt16 Converts a type to an unsigned int type. ToUInt32 Converts a type to an unsigned long type. ToUInt64 Converts a type to an unsigned big integer.
  • 7. • structure is a collection of predefined methods • 1.minvalue() • 2.maxvalue() • 3.tostring() • 4.parse() • working with parse() is called as parsing. parse() method can be used only to convert from strings into int. tostring() can be used only to convert from int to string. • ex: print the limits of datatypes. • static void Main(string[] args) • { Console.WriteLine(byte.MinValue.ToString()); • Console.WriteLine(byte.MaxValue.ToString()); • Console.WriteLine(int.MinValue.ToString()); • Console.WriteLine(int.MaxValue.ToString()); • Console.ReadLine(); }
  • 8. • 2.example on parsing • 1.take the form • 2.add 3 labels,3 textboxes,1 button control • private void button1_Click(object sender, EventArgs e) • { textBox3.Text = textBox1.Text + textBox2.Text; } • o/p:1010 • it gives concatenation error • ex: • "10"+"10"=1010 • 10+10=20; • we can overcome this problem using parsing method.. • textBox3.Text = (int.Parse(textBox1.Text) +int.Parse(textBox2.Text)).ToString();
  • 9. • 2 nd method: • private void button1_Click(object sender, EventArgs e) • { // textBox3.Text = (int.Parse(textBox1.Text) +int.Parse(textBox2.Text)).ToString(); • int a, b, c; • a = int.Parse(textBox1.Text); • b = int.Parse(textBox2.Text); • c = a + b; • textBox3.Text = c.ToString(); • }
  • 10. • Working with converting method: • working with convert class is called as converting. • convert is the part fcl(frame work class library) • convert is the collection of predefined methods • convert.toint16()-short-2 bytes • convert.toint32()-int-4 bytes • convert.toint64()-long-8 bytes • convert.tochar() • convert.tobyte() • convert.toboolean() • convert.tofloat() • convert.todouble()
  • 11. • Example on converting: • take the form • 2.add 3 labels,3 textboxes,1 button control • private void button1_Click(object sender, EventArgs e) • { textBox3.Text = textBox1.Text + textBox2.Text; } • o/p:1010 • it gives concatenation error • ex:"10"+"10"=1010 • 10+10=20; • we can overcome this problem using parsing method.. • textBox3.Text = (convert.toint32(textBox1.Text) +convert.toint32(textBox2.Text)).ToString();
  • 12. • 2 nd method: • private void button1_Click(object sender, EventArgs e) • { • // textBox3.Text = (int.Parse(textBox1.Text) +int.Parse(textBox2.Text)).ToString(); • int a, b, c; • a = convert.toint16(textBox1.Text); • b = convert.toint16(textBox2.Text); • c = a + b; • textBox3.Text = c.ToString(); • }
  • 13. For more visit our website www.siri-kt.blogspot.com Thanks for Watching More Angular JS TutorialsMore C sharp (c#) tutorials