Domains
Artificial Intelligence
Harness AI to create smarter workflows and scalable growth.
View All Artificial Intelligence Coursesicon-aiCertifications
Certification
100+ Hours
Trending
Post Graduate Certification Program in Data Analytics and Applied AICertification
140+ Hours
Trending
Executive Post Graduate Certificate in Data Science and Applied AICertification
140+ Hours
Trending
PG Certificate in Applied Generative Engineering & LLM ApplicationsCertification
150+ Hours
Trending
PG Certificate in AI Powered Product and Design ThinkingCertification
15 Weeks
Trending
AI Masters ProgramCertification
4 Weeks
Trending
Vibe Coding 101: No-code AI ProgrammingCertification
48 Hours
Trending
Applied Agentic AI - No CodeCertification
16 Hours
Trending
Generative AI and Prompt EngineeringCertification
8 Weeks
Trending
AI-Powered Product ManagementCertification
8 Weeks
Trending
Applied Agentic AI CertificationCertification
16 Hours
Generative AI Course for Scrum MastersCertification
16 Hours
Generative AI Course for Project ManagersCertification
16 Hours
Generative AI Course for POPMCertification
16 Hours
Gen AI Course for Business AnalystsCertification
16 Hours
AI Powered Software DevelopmentCertification
16 Hours
AI-Data Analytics with Power BICertification
16 Hours
AI-Driven Digital Marketing TrainingCertification
16 Hours
Gen AI for Enterprise AgilistAdvanced Certifications
Masters
Executive Diploma
Executive Diploma in Machine Learning and AIExecutive Diploma
Executive Diploma in Data Science & Artificial Intelligence from IIITBCertification
Chief Technology Officer & AI Leadership ProgrammeMaster's Degree
Master of Science in Machine Learning & AIDual Certification
Executive Programme in Generative AI for LeadersCertification
Executive Post Graduate Programme in Applied AI and Agentic AIExecutive PG Program
IIT KGP-Executive PG Certificate in Gen AI and AgenticSelf-Learning Courses
Universal AI by MIT Open LearningAgile 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
AI-Empowered SAFe® 6.0 Scrum MasterPMI
21 Hours
Best Seller
PMI Agile Certified Practitioner (PMI-ACP) CertificationAdvanced Certifications
Scaled Agile, Inc.
32 Hours
Recommended
Implementing SAFe 6.0 (SPC) CertificationScaled Agile, Inc.
24 Hours
AI-Empowered SAFe® 6 Release Train Engineer (RTE) CourseScaled Agile, Inc.
16 Hours
Trending
SAFe® AI-Empowered Product Owner/Product Manager (6.0)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 MasterProduct OwnerSAFe AgilistAgile CoachAccreditation Bodies
Scrum AllianceScaled Agile, Inc.Scrum.orgICAgilePMITop Resources
Scrum TutorialCSM VS CSPOCSM vs PSMScrum Practice TestPO Interview QsProject 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 CertificationPMI
23 Hours
Best Seller
Certified Associate in Project Management (CAPM)®PMI
24 Hours
Best Seller
Program Management Professional (PgMP®)PMI
24 Hours
Best Seller
Portfolio Management Professional (PfMP)®PMI
30 Hours
Best Seller
Project Management Institute-Risk Management Professional (PMI-RMP)®Skills
Change ManagementProject Management TechniquesCertified Associate in Project Management (CAPM) CertificationOracle Primavera P6 CertificationMasters
Job Oriented
45 Hours
Trending
Project Management Master's ProgramOn-Demand Courses
PRINCE2 Practitioner CoursePRINCE2 Foundation CourseRoles
Project ManagerProgram Management ProfessionalPortfolio Management ProfessionalAccreditation Bodies
PMIAxelosScrum AllianceTop Resources
Theories of MotivationManagement ProcessProject Manager Intvw QsWhat is PMP?PMP Practice TestCyber 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 v13) CertificationISACA
40 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) CertificationISACA
16 Hours
COBIT5 FoundationOn-Demand Courses
CISSPTop Resources
Laptops for IT SecurityIT Security Thesis TopicEthical Hacking Intvw QsCybersecurity in BankingBasics of CybersecurityCloud 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 CertificationAWS
24 Hours
Developing on AWSMasters
Job Oriented
48 Hours
New
AWS Cloud Architect Masters ProgramRoles
Cloud EngineerCloud ArchitectOn-Demand Courses
AWS Certified Developer Associate - Complete GuideAWS Certified DevOps EngineerAWS Certified Solutions Architect AssociateMicrosoft Certified Azure Data Engineer AssociateMicrosoft Azure Administrator (AZ-104) CourseAWS Certified SysOps Administrator AssociateMicrosoft Certified Azure Developer AssociateAWS Certified Cloud PractitionerAuthorized Partners of
AWSMicrosoftTop Resources
Scrum TutorialCSM VS CSPOCSM vs PSMScrum Practice TestPO Interview QsIT Service Management
Understand how to plan, design, and optimize IT services efficiently.
View All ITSM Coursesicon-git-commitCertifications
Axelos
16 Hours
New
ITIL Foundation (Version 5) CertificationAxelos
16 Hours
Best Seller
ITIL 4 Foundation CertificationAxelos
8 Hours
New
ITIL Foundation Bridge Course (Version 5)PeopleCert
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 ExamITIL 4 Specialist: Drive Stakeholder Value (DSV) CourseITIL 4 Strategist: Direct, Plan, and ImproveITIL 4 FoundationTop Resources
ITIL Practice TestITSM Key ConceptsWhat is ITIL?Top SysAdmin SkillsTop Service-based FirmsData Science
Unlock valuable insights from data with advanced analytics.
View All Data Science Coursesicon-dataOur Courses
Data Science with PythonSQL For Data AnalyticsRoles
Data ScientistData AnalystData EngineerAI EngineerOn-Demand Courses
Data Analysis Using ExcelDeep Learning with Keras and TensorFlowDeployment of Machine Learning ModelsFundamentals of Reinforcement LearningIntroduction to Cutting-Edge AI with TransformersMachine Learning with PythonMaster Python: Advance Data Analysis with PythonMaths and Stats FoundationNatural Language Processing (NLP) with PythonPython for Data ScienceSQL for Data Analytics CoursesAI Advanced: Computer Vision for AI ProfessionalsMaster Applied Machine LearningMaster Time Series Forecasting Using PythonTop Resources
Machine Learning TutorialData Science Interview QuestionsDevOps
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
KubernetesDocker with KubernetesJenkinsRoles
DevOps EngineerDevOps ExpertOn-Demand Courses
CI/CD with Jenkins XDevOps Using JenkinsCI-CD and DevOpsDocker & KubernetesDevOps Fundamentals Crash CourseMicrosoft Certified DevOps Engineer ExpertAnsible for Beginners: The Complete Crash CourseContainer Orchestration Using KubernetesContainerization Using DockerMaster Infrastructure Provisioning with TerraformGlobal Accreditations
DevOps InstituteTop Resources
Top DevOps ProjectsDevOps Practice TestDevOps Interview QsGit TutorialBasic Docker CommandsBI 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 CertificationOn-Demand Courses
Data Visualization Using Tableau TrainingData Analysis Using ExcelTop Resources
Python Data Viz LibsSpotfire Interview QsTop Benefits of Power BIPower BI Dev FutureWeb Development
Learn to create user-friendly, fast, and dynamic web applications.
View All Web Development Coursesicon-codeOur Courses
ReactNode JSAngularOn-Demand Courses
Angular TrainingBasics of Spring Core and MVCFront-End Development BootcampReact JS TrainingSpring Boot and Spring CloudMongoDB Developer CourseTop Resources
Top HTML ProjectsFED Interview QsReact Native Interview QsBlockchain
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 CertificationOn-Demand Courses
NFT Essentials 101: A Beginner's GuideIntroduction to DeFiTop 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 JavaScriptData Structures and Algorithms with Java: The Practical GuideLinux Essentials for Developers: The Complete MasterclassMaster Git and GitHubMaster Java Programming LanguageProgramming Essentials for BeginnersSoftware Engineering Fundamentals and Lifecycle (SEFLC) CourseTest-Driven Development for Java ProgrammersTypeScript: Beginner to AdvancedTop Resources
Python TutorialR Programming TutorialSpring Boot Interview QsType 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