
Domains
Agile Management
Master Agile methodologies for efficient and timely project delivery.
View All Agile Management Coursesicon-refresh-cwCertifications
Scrum Alliance
16 Hours
Best Seller
Certified ScrumMaster (CSM) CertificationScrum Alliance
16 Hours
Best Seller
Certified Scrum Product Owner (CSPO) CertificationScaled Agile
16 Hours
Trending
Leading SAFe 6.0 CertificationScrum.org
16 Hours
Professional Scrum Master (PSM) CertificationScaled Agile
16 Hours
SAFe 6.0 Scrum Master (SSM) CertificationAdvanced Certifications
Scaled Agile, Inc.
32 Hours
Recommended
Implementing SAFe 6.0 (SPC) CertificationScaled Agile, Inc.
24 Hours
SAFe 6.0 Release Train Engineer (RTE) CertificationScaled Agile, Inc.
16 Hours
Trending
SAFe® 6.0 Product Owner/Product Manager (POPM)IC Agile
24 Hours
ICP Agile Certified Coaching (ICP-ACC)Scrum.org
16 Hours
Professional Scrum Product Owner I (PSPO I) TrainingMasters
32 Hours
Trending
Agile Management Master's Program32 Hours
Agile Excellence Master's ProgramOn-Demand Courses
Agile and ScrumRoles
Scrum MasterTech Courses and Bootcamps
Full Stack Developer BootcampAccreditation Bodies
Scrum AllianceTop Resources
Scrum TutorialProject Management
Gain expert skills to lead projects to success and timely completion.
View All Project Management Coursesicon-standCertifications
PMI
36 Hours
Best Seller
Project Management Professional (PMP) CertificationAxelos
32 Hours
PRINCE2 Foundation & Practitioner CertificationAxelos
16 Hours
PRINCE2 Foundation CertificationAxelos
16 Hours
PRINCE2 Practitioner CertificationSkills
Change ManagementMasters
Job Oriented
45 Hours
Trending
Project Management Master's ProgramUniversity Programs
45 Hours
Trending
Project Management Master's ProgramOn-Demand Courses
PRINCE2 Practitioner CourseRoles
Project ManagerAccreditation Bodies
PMITop Resources
Theories of MotivationCloud Computing
Learn to harness the cloud to deliver computing resources efficiently.
View All Cloud Computing Coursesicon-cloud-snowingCertifications
AWS
32 Hours
Best Seller
AWS Certified Solutions Architect - AssociateAWS
32 Hours
AWS Cloud Practitioner CertificationAWS
24 Hours
AWS DevOps CertificationMicrosoft
16 Hours
Azure Fundamentals CertificationMicrosoft
24 Hours
Best Seller
Azure Administrator CertificationMicrosoft
45 Hours
Recommended
Azure Data Engineer CertificationMicrosoft
32 Hours
Azure Solution Architect CertificationMicrosoft
40 Hours
Azure DevOps CertificationAWS
24 Hours
Systems Operations on AWS Certification TrainingAWS
24 Hours
Developing on AWSMasters
Job Oriented
48 Hours
New
AWS Cloud Architect Masters ProgramBootcamps
Career Kickstarter
100 Hours
Trending
Cloud Engineer BootcampRoles
Cloud EngineerOn-Demand Courses
AWS Certified Developer Associate - Complete GuideAuthorized Partners of
AWSTop Resources
Scrum TutorialIT Service Management
Understand how to plan, design, and optimize IT services efficiently.
View All DevOps Coursesicon-git-commitCertifications
Axelos
16 Hours
Best Seller
ITIL 4 Foundation CertificationAxelos
16 Hours
ITIL Practitioner CertificationPeopleCert
16 Hours
ISO 14001 Foundation CertificationPeopleCert
16 Hours
ISO 20000 CertificationPeopleCert
24 Hours
ISO 27000 Foundation CertificationAxelos
24 Hours
ITIL 4 Specialist: Create, Deliver and Support TrainingAxelos
24 Hours
ITIL 4 Specialist: Drive Stakeholder Value TrainingAxelos
16 Hours
ITIL 4 Strategist Direct, Plan and Improve TrainingOn-Demand Courses
ITIL 4 Specialist: Create, Deliver and Support ExamTop Resources
ITIL Practice TestData Science
Unlock valuable insights from data with advanced analytics.
View All Data Science Coursesicon-dataBootcamps
Job Oriented
6 Months
Trending
Data Science BootcampJob Oriented
289 Hours
Data Engineer BootcampJob Oriented
6 Months
Data Analyst BootcampJob Oriented
288 Hours
New
AI Engineer BootcampSkills
Data Science with PythonRoles
Data ScientistOn-Demand Courses
Data Analysis Using ExcelTop Resources
Machine Learning TutorialDevOps
Automate and streamline the delivery of products and services.
View All DevOps Coursesicon-terminal-squareCertifications
DevOps Institute
16 Hours
Best Seller
DevOps Foundation CertificationCNCF
32 Hours
New
Certified Kubernetes AdministratorDevops Institute
16 Hours
Devops LeaderSkills
KubernetesRoles
DevOps EngineerOn-Demand Courses
CI/CD with Jenkins XGlobal Accreditations
DevOps InstituteTop Resources
Top DevOps ProjectsBI And Visualization
Understand how to transform data into actionable, measurable insights.
View All BI And Visualization Coursesicon-microscopeBI and Visualization Tools
Certification
24 Hours
Recommended
Tableau CertificationCertification
24 Hours
Data Visualization with Tableau CertificationMicrosoft
24 Hours
Best Seller
Microsoft Power BI CertificationTIBCO
36 Hours
TIBCO Spotfire TrainingCertification
30 Hours
Data Visualization with QlikView CertificationCertification
16 Hours
Sisense BI CertificationOn-Demand Courses
Data Visualization Using Tableau TrainingTop Resources
Python Data Viz LibsCyber Security
Understand how to protect data and systems from threats or disasters.
View All Cyber Security Coursesicon-refresh-cwCertifications
CompTIA
40 Hours
Best Seller
CompTIA Security+EC-Council
40 Hours
Certified Ethical Hacker (CEH v12) CertificationISACA
22 Hours
Certified Information Systems Auditor (CISA) CertificationISACA
40 Hours
Certified Information Security Manager (CISM) Certification(ISC)²
40 Hours
Certified Information Systems Security Professional (CISSP)(ISC)²
40 Hours
Certified Cloud Security Professional (CCSP) Certification16 Hours
Certified Information Privacy Professional - Europe (CIPP-E) CertificationISACA
16 Hours
COBIT5 Foundation16 Hours
Payment Card Industry Security Standards (PCI-DSS) CertificationOn-Demand Courses
CISSPTop Resources
Laptops for IT SecurityWeb Development
Learn to create user-friendly, fast, and dynamic web applications.
View All Web Development Coursesicon-codeBootcamps
Career Kickstarter
6 Months
Best Seller
Full-Stack Developer BootcampJob Oriented
3 Months
Best Seller
UI/UX Design BootcampEnterprise Recommended
6 Months
Java Full Stack Developer BootcampCareer Kickstarter
490+ Hours
Front-End Development BootcampCareer Accelerator
4 Months
Backend Development Bootcamp (Node JS)Skills
ReactOn-Demand Courses
Angular TrainingTop Resources
Top HTML ProjectsBlockchain
Understand how transactions and databases work in blockchain technology.
View All Blockchain Coursesicon-stop-squareBlockchain Certifications
40 Hours
Blockchain Professional Certification32 Hours
Blockchain Solutions Architect Certification32 Hours
Blockchain Security Engineer Certification24 Hours
Blockchain Quality Engineer Certification5+ Hours
Blockchain 101 CertificationOn-Demand Courses
NFT Essentials 101: A Beginner's GuideTop Resources
Blockchain Interview QsProgramming
Learn to code efficiently and design software that solves problems.
View All Programming Coursesicon-codeSkills
Python CertificationInterview Prep
Career Accelerator
3 Months
Software Engineer Interview PrepOn-Demand Courses
Data Structures and Algorithms with JavaScriptTop Resources
Python TutorialType conversion or type casting involves converting one data type to another. There are mainly two types of type conversion in C#. These are given as follows:
Implicit type conversion is performed by the compiler implicitly in a way that is type-safe. These types of conversions can include char to int, int to float etc. Conversions from derived classes to base classes are also a part of implicit type conversion.
A program that demonstrates implicit type conversion is given as follows:
using System;
namespace ImplicitConversionDemo
{
class Example
{
static void Main(string[] args)
{
int a = 5;
int b = 3;
float c;
c = a * b;
Console.WriteLine("Product of {0} and {1} is {2}", a, b, c);
}
}
}
Source Code: Program that demonstrates implicit type conversion in C#
The output of the above program is as follows:
Product of 5 and 3 is 15
Now let us understand the above program.
In the program, a and b are int variables that contain the values 5 and 3 respectively. They are multiplied and the resultant value in stored in c which is of type double. This shows implicit type conversion. Then the values of a, b and c are displayed. The code snippet for this is as follows:
int a = 5;
int b = 3;
double c;
c = a * b;
Console.WriteLine("Product of {0} and {1} is {2}", a, b, c);
Explicit type conversion is performed by the compiler explicitly by requesting the conversion of a data type into another data type. This requires a cast operator as the compiler is forced to make the transformation.
Cast operators are unsafe and can lead to the loss of data if a larger data type is converted into a smaller data type.
A program that demonstrates explicit type conversion is given as follows:
using System;
namespace ExplicitConversionDemo
{
class Example
{
static void Main(string[] args)
{
double a = 7.5;
double b = 3.5;
double c;
int d;
c = a * b;
d = (int)c;
Console.WriteLine("Product of {0} and {1} is {2}", a, b, c);
Console.WriteLine("Value after Explicit Type Conversion is {0}", d);
}
}
}
Source Code: Program that demonstrates explicit type conversion in C#
The output of the above program is as follows:
Product of 7.5 and 3.5 is 26.25
Value after Explicit Type Conversion is 26
Now let us understand the above program.
In the program, a and b are double variables that contain the values 7.5 and 3.5 respectively. They are multiplied and the resultant value in stored in c which is also of type double. Then the value of c is stored in d using explicit type casting as d is of type int.
Then the values of a, b and c are displayed. Also, the value stored in d after explicit type casting is also displayed. The code snippet for this is as follows:
double a = 7.5;
double b = 3.5;
double c;
int d;
c = a * b;
d = (int)c;
Console.WriteLine("Product of {0} and {1} is {2}", a, b, c);
Console.WriteLine("Value after Explicit Type Conversion is {0}", d);
Let us now see some examples of Type Conversions and Conversions in C# using the Convert class:
If you want to convert Integer to String in C#, then use the ToString() method that represents any value as a string.
Let us now see an example:
using System;
class Example {
static void Main(string[] args)
{
String str;
int val = 130;
str = val.ToString();
Console.WriteLine("String = "+str);
Console.ReadLine();
}
}
Source Code: Program that converts Integer to String in C#
The above program generates the following output:
String = 130
To convert binary value to decimal, let us see the code. Our binary value here is 11100:
using System;
using System.Collections.Generic;
using System.Text;
namespace BinDecExample
{
class Example
{
static void Main(string[] args)
{
int n, rem, bin;
int dec = 0, baseValue = 1;
n = 11100;
Console.Write("\nBinary: "+n);
bin = n;
while (n > 0)
{
rem = n % 10;
dec = dec + rem * baseValue;
n = n / 10 ;
baseValue = baseValue * 2;
}
Console.Write("\nConverted to Decimal: "+dec);
Console.ReadLine();
}
}
}
Source Code: Program that converts binary value to decimal in C#
The above program generates the following output:
Binary: 11100
Converted to Decimal: 28
To convert Decimal value to Octal, let us see the following code:
using System;
namespace DecOctExample {
class Example {
static void Main(string[] args) {
int i = 0;
int []val = new int[30];
int dec = 30;
Console.WriteLine("Decimal...\n"+dec);
while (dec != 0)
{
val[i] = dec % 8;
dec = dec / 8;
i++;
}
Console.WriteLine("Octal...");
for (int k = i - 1; k >= 0; k--)
Console.Write(val[k]);
Console.ReadKey();
}
}
}
Source Code: Program that converts Decimal value to Octal in C#
The above program generates the following output:
Decimal...
30
Octal...
36
To convert float to binary, the following code is displayed below:
using System;
using System.IO;
using System.CodeDom.Compiler;
namespace FloatBinExample {
class Example {
static void Main(string[] args) {
float f = 22.3f;
Console.WriteLine("Float = "+f);
string str = "";
while (f >= 1) {
str = (f % 2) + str;
f = f / 2;
}
Console.Write(str);
}
}
}
Source Code: Program that converts float to binary in C#
The above program generates the following output:
Float = 22.3
1.393750.78749991.5751.150.2999992
To convert string to int, the Int32.Parse() method is used. Let us see how the method is used for conversion:
using System;
class Example {
static void Main() {
string str ="877";
int res = Int32.Parse(str);
Console.WriteLine("int = "+res);
}
}
Source Code: Program that converts string to int in C#
The above program generates the following output:
int = 877
The Convert.ToSByte() method is used to convert hex string to hex number. Do not forget to set the radix as 16, since Hexadecimal is represented by 16 radix.
Let us see an example now:
using System;
namespace Example {
public class Demo {
public static void Main(string[] args) {
string hexStr = "3E";
Console.WriteLine("Hex String = "+hexStr);
Console.WriteLine("Hex = "+Convert.ToSByte(hexStr, 16));
}
}
}
Source Code: Program that converts hex string to hex number in C#
The above program generates the following output:
Hex String = 3E
Hex = 62
The Bool.parse() method is used in C# to convert string to bool. Let us see an example:
using System;
using System.Linq;
class Example
{
static void Main()
{
string myStr = "true";
bool b = bool.Parse(myStr);
Console.WriteLine(b);
}
}
Source Code: Program that converts string to bool in C#
The above program generates the following output:
TrueThe Long.parse() method is to be used in C# to convert string to long.
Let us see an example:
using System;
using System.Linq;
class Example
{
static void Main()
{
string s = "7887687687";
long res = long.Parse(s);
Console.WriteLine(res);
}
}
Source Code: Program that converts string to long in C#
The above program generates the following output:
7887687687The Convert.ToDecimal() method is used in C# to converts 32-bit signed integer to decimal in C#:
using System;
public class Demo {
public static void Main() {
int i = 866;
Console.WriteLine(" Int32 = "+ i);
decimal res = Convert.ToDecimal(i);
Console.WriteLine(" Converted to decimal = "+ res);
}
}
Source Code: Program that converts 32-bit signed integer to decimal in C#
The above program generates the following output:
Int32 = 866
Converted to decimal = 866
The Convert.ToInt32() method is used in C# to convert Byte value to Int32 value. Let us see an example:
using System;
public class Example {
public static void Main() {
byte b = 100;
int res = Convert.ToInt32(b);
Console.WriteLine("Converted byte to Int32 = "+res);
}
}
Source Code: Program that converts Byte value to Int32 value in C#
The above program generates the following output:
Converted byte to Int32 = 100
The Convert.ToInt64() method is used to convert a Double value to Int64 value:
using System;
public class Example {
public static void Main() {
double d = 35.873628e12;
long res = Convert.ToInt64(d);
Console.WriteLine("Converted = {0:E} to {1:N0} ", d, res);
}
}
Source Code: Program that converts Double value to Int64 value in C#
The above program generates the following output:
Converted = 3.587363E+013 to 35,873,628,000,000
The Convert.ToInt32() method is used in C# to convert Double to an Integer value:
using System;
public class Example {
public static void Main() {
double d = 34.23;
int i = Convert.ToInt32(d);
Console.WriteLine("Converted {0} to {1} ", d, i);
}
}
Source Code: Program that converts Double to an Integer in C#
The above program generates the following output:
Converted 34.23 to 34
The Convert class in C# converts one datatype to another. Let us see some example of the conversion methods:
The Convert.ToInt16() method is used to convert a value to a 16-bit signed integer.
Let us see an example:
using System;
public class Example {
public static void Main() {
double d = 1.955;
short s;
Console.WriteLine("Converting...");
s = Convert.ToInt16(d);
Console.WriteLine("Converted {0} to {1}", d, s);
}
}
Source Code: Program that demonstrates Convert.ToInt16() in C#
The above program generates the following output:
Converting...
Converted 1.955 to 2
The Convert.ToUInt16() method is used to convert a value to a 16-bit unsigned integer.
Let us see an example:
using System;
public class Example {
public static void Main() {
string s = "19";
ushort us;
Console.WriteLine("Converting...");
// converting
us = Convert.ToUInt16(s);
Console.WriteLine("Converted string '{0}' to {1}", s, us);
}
}
Source Code: Program that demonstrates Convert.ToUInt16() in C#
The above program generates the following output:
Converting...
Converted string '19' to 19
The Convert.ToDecimal() method is used to convert a value to a decimal number. Let us see an example:
using System;
public class Example {
public static void Main() {
decimal dec;
string str = "7,294.56";
System.Console.WriteLine("String = "+str);
dec = System.Convert.ToDecimal(str);
System.Console.WriteLine("Converted to decimal = {0} ", dec);
}
}
Source Code: Program that demonstrates Convert.ToDecimal() in C#
The above program generates the following output:
String = 7,294.56
Converted to decimal = 7294.56
The Convert.ToInt64() method is to be used for converting Decimal to Int64 in C#. Let us see an example:
using System;
class Example
{
static void Main()
{
decimal d = 245.54m;
long res;
Console.WriteLine("Decimal = "+d);
res = Convert.ToInt64(d);
Console.WriteLine("Int64 = "+ res);
}
}
Source Code: Program that demonstrates Convert.ToInt64() in C#
The above program generates the following output:
Decimal = 245.54
Int64 = 246
The ToSingle() method is used to convert a value to a single-precision floating-point:
Let us see an example:
using System;
public class Example {
public static void Main() {
bool b = true;
float f;
Console.WriteLine("Bool: "+b);
f = Convert.ToSingle(b);
Console.WriteLine("Converted: {0} to {1}", b, f);
}
}
Source Code: Program that demonstrates Convert.ToSingle() in C#
The above program generates the following output:
Bool: True
Converted: True to 1
Convert a specified value to a Unicode integer using ToChar() method. Let us see an example:
The using System;
public class Demo {
public static void Main() {
sbyte[] myByte = { 34, 45, 87, 101, 102, 103 };
char ch;
foreach (sbyte sb in myByte)
{
ch = Convert.ToChar(sb);
Console.WriteLine("{0} converted to '{1}'", sb, ch);
}
}
}
Source Code: Program that demonstrates Convert.ToChar() in C#
The above program generates the following output:
34 converted to '"'
45 converted to '-'
87 converted to 'W'
101 converted to 'e'
102 converted to 'f'
103 converted to 'g'
To convert a given value to DateTime value, use the Convert.ToDateTime method.
Let us see an example:
using System;
public class Example {
public static void Main() {
string str;
DateTime dt;
str = "11/11/2018";
Console.WriteLine("String = "+str);
dt = Convert.ToDateTime(str);
Console.WriteLine("Converted to = "+dt);
}
}
Source Code: Program that demonstrates Convert.ToDateTime() in C#
The above program generates the following output:
String = 11/11/2018
Converted to = 11/11/2018 12:00:00 AM
The Convert.ToInt32() method is to be used in C# to convert a value to a 32-bit signed integer.
Let us see an example:
using System;
public class Demo {
public static void Main() {
double d = 25.68;
Console.WriteLine("Double value = "+d);
int i;
i = Convert.ToInt32(d);
Console.WriteLine("Converted {0} to {1}", d, i);
}
}
Source Code: Program that demonstrates Convert.ToInt32() in C#
The above program generates the following output:
Double value = 25.68
Converted 25.68 to 26
The Convert.ToInt64() method is to be used in C# to convert a value to a 64-bit signed integer.
Let us see an example:
using System;
public class Demo {
public static void Main() {
double d = 588.654;
Console.WriteLine("Double: "+d);
long l;
l = Convert.ToInt32(d);
Console.WriteLine("Converted to long = "+l);
}
}
Source Code: Program that demonstrates Convert.ToInt64() in C#
The above program generates the following output:
Double: 588.654
Converted to long = 589
The Convert.ToSByte() method is used to convert a value to SByte. The SByte type is an 8-bit signed integer.
Let us see an example:
using System;
public class Example {
public static void Main() {
double d = 30.6;
Console.WriteLine("Double = "+d);
sbyte sb;
sb = Convert.ToSByte(d);
Console.WriteLine("Converted to SByte = "+sb);
}
}
Source Code: Program that demonstrates Convert.ToSByte() in C#
The above program generates the following output:
Double = 30.6
Converted to SByte = 31
The Convert.ToDouble() method is used to convert a value to a double-precision floating-point number.
using System;
public class Demo {
public static void Main() {
long[] val = { 87687, 154416546, -8768767};
double d;
foreach (long a in val)
{
Console.WriteLine(a);
}
Console.WriteLine("Converted to...");
foreach (long a in val)
{
d = Convert.ToDouble(a);
Console.WriteLine(d);
}
}
}
Source Code: Program that demonstrates Convert.ToDouble() in C#
The above program generates the following output:
87687
154416546
-8768767
Converted to...
87687
154416546
-8768767
The ToString() method is used in C# to convert the value to its equivalent string
Let us see an example:
using System;
public class Demo {
public static void Main() {
bool b = true;
Console.WriteLine(b);
Console.WriteLine(Convert.ToString(b));
}
}
Source Code: Program that demonstrates Convert.ToString() in C#
The above program generates the following output:
True
True
To convert a specified value to an equivalent Boolean value, use the Convert.ToBoolean() method.
Let us see an example:
using System;
public class Example {
public static void Main() {
double d = 89.767;
bool b;
Console.WriteLine("Double: "+d);
b = System.Convert.ToBoolean(d);
Console.WriteLine("Converted to Boolean equivalent: "+b);
}
}
Source Code: Program that demonstrates Convert.ToBoolean() in C#
The above program generates the following output:
Double: 89.767
Converted to Boolean equivalent: True
Convert a specified value to an 8-bit unsigned integer using the Convert.ToByte() method.
Let us see an example:
using System;
public class Example {
public static void Main() {
char[] ch = { 'a', 'b'};
Console.WriteLine("Values...");
foreach (char c in ch)
{
Console.WriteLine(c);
}
Console.WriteLine("Conversion...");
foreach (char c in ch)
{
byte b = Convert.ToByte(c);
Console.WriteLine("{0} Converted to Byte = {1} ",c, b);
}
}
}
Source Code: Program that demonstrates Convert.ToByte() in C#
The above program generates the following output:
Values...
a
b
Conversion...
a Converted to Byte = 97
b Converted to Byte = 98
Convert a value to a 64-bit unsigned integer using the Convert.ToUInt64() method.
Let us see an example:
using System;
public class Example {
public static void Main() {
char c = 'y';
ulong ul;
Console.WriteLine("Char: "+c);
ul = Convert.ToUInt64(c);
Console.WriteLine("After conversion: "+ul);
}
}
Source Code: Program that demonstrates Convert.ToUInt64() in C#
The above program generates the following output:
Char: y
After conversion: 121
The Convert.ToUInt32() method is used to convert a specified value to a 32-bit unsigned integer.
Let us see an example:
using System;
public class Example {
public static void Main() {
string s = "30";
uint ui;
ui = Convert.ToUInt32(s);
Console.WriteLine("Converted value: "+ui);
}
}
Source Code: Program that demonstrates Convert.ToUInt32() in C#
The following is the output:
Converted value: 30