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

Computer data type and Terminologies
Computer data type and Terminologies Computer data type and Terminologies
Computer data type and Terminologies
glyvive
 
Stream Based Input Output
Stream Based Input OutputStream Based Input Output
Stream Based Input Output
Bharat17485
 

What's hot (14)

Intake 37 6
Intake 37 6Intake 37 6
Intake 37 6
 
Learning python
Learning pythonLearning python
Learning python
 
Intake 37 5
Intake 37 5Intake 37 5
Intake 37 5
 
MD-5 : Algorithm
MD-5 : AlgorithmMD-5 : Algorithm
MD-5 : Algorithm
 
C Omega
C OmegaC Omega
C Omega
 
30csharp
30csharp30csharp
30csharp
 
Object oriented programming 9 expressions and type casting in c++
Object oriented programming 9 expressions and type casting in c++Object oriented programming 9 expressions and type casting in c++
Object oriented programming 9 expressions and type casting in c++
 
Computer data type and Terminologies
Computer data type and Terminologies Computer data type and Terminologies
Computer data type and Terminologies
 
SHA 1 Algorithm
SHA 1 AlgorithmSHA 1 Algorithm
SHA 1 Algorithm
 
5variables in c#
5variables in c#5variables in c#
5variables in c#
 
SHA- Secure hashing algorithm
SHA- Secure hashing algorithmSHA- Secure hashing algorithm
SHA- Secure hashing algorithm
 
MD5
MD5MD5
MD5
 
Aes128 bit project_report
Aes128 bit project_reportAes128 bit project_report
Aes128 bit project_report
 
Stream Based Input Output
Stream Based Input OutputStream Based Input Output
Stream Based Input Output
 

Similar to 19c

Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
Ronaldo Aditya
 

Similar to 19c (20)

Ifi7184.DT lesson 2
Ifi7184.DT lesson 2Ifi7184.DT lesson 2
Ifi7184.DT lesson 2
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Programming using c++ tool
Programming using c++ toolProgramming using c++ tool
Programming using c++ tool
 
What is Data Types and Functions?
What is Data Types and Functions?What is Data Types and Functions?
What is Data Types and Functions?
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
 
BASICS OF PYTHON usefull for the student who would like to learn on their own
BASICS OF PYTHON usefull for the student who would like to learn on their ownBASICS OF PYTHON usefull for the student who would like to learn on their own
BASICS OF PYTHON usefull for the student who would like to learn on their own
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
 
Getting started cpp full
Getting started cpp   fullGetting started cpp   full
Getting started cpp full
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python Programming
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Data types in c++
Data types in c++ Data types in c++
Data types in c++
 

More from Sireesh K (20)

Cn10
Cn10Cn10
Cn10
 
chanakya neeti
chanakya neetichanakya neeti
chanakya neeti
 
chanakya neeti
chanakya neetichanakya neeti
chanakya neeti
 
What is mvc
What is mvcWhat is mvc
What is mvc
 
31c
31c31c
31c
 
31cs
31cs31cs
31cs
 
45c
45c45c
45c
 
44c
44c44c
44c
 
43c
43c43c
43c
 
42c
42c42c
42c
 
41c
41c41c
41c
 
40c
40c40c
40c
 
39c
39c39c
39c
 
38c
38c38c
38c
 
37c
37c37c
37c
 
35c
35c35c
35c
 
34c
34c34c
34c
 
33c
33c33c
33c
 
30c
30c30c
30c
 
29c
29c29c
29c
 

Recently uploaded

Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 

Recently uploaded (20)

Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 

19c

  • 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