Casting VS Converting VS Parsing in C#: Which to choose?

preview_player
Показать описание
In this video we will discuss the difference between converting, parsing, and casting in C#

Welcome back to a new C# tutorial, in this tutorial we will discuss the difference between converting, parsing, and casting in C#.

This might be a confusing topic since not a lot of developers know the exact difference between the 3 methods, also this might pop up as an interview question ;)

What is casting or typecasting exactly?.

Now to be more technical casting can be done using a Cast Expression.

A Cast expression is used to perform an explicit conversion from one type to another.

So let’s see an example of a cast expression.

A cast expression of the form (T)E performs an explicit conversion of the result of expression E to type T.

The main difference between the convert and the parse methods are the following:

Convert takes an object as an input/argument where parse takes a string.

The convert methods will never throw an ArgumentNullException if you passed null as an argument, it will simply return the default value for the value type you are trying to convert to, for example 0 for integers, where Parse will throw an exception if you pass it null.

Now when it comes to converting strings both the convert and the parse methods will do the same thing, so in that case I recommend using Parse/TryParse.

But…

If you are dealing with your own custom types then, you can implement the IConvertible interface to provide your own implementation and use the Convert method to convert from other types to your own custom types.

tutorialsEU offers you free video tutorials about programming and development for complete beginners up to experienced programmers.
This includes C#, Unity, Python, Android, Kotlin, Machine Learning, etc.
Рекомендации по теме
Комментарии
Автор

Glad that you keep us updated on YouTube too. Keep up the GoodWork. :)

HawkEncounters
Автор

Hey,

I noticed you didn't include details of Casting VS Converting VS Parsing in your UDEMY master class course. I think it's an important video, so pls add it to the course

GalaxyCookies
Автор

2:24 2 years ago Visual Studio had code suggestions without those Copilots. But that's gone now.

firstlast
Автор

Thanks for sharing! I have a list of strings, I need to convert some of those values to Decimals, I have a class called "Promedio" with some values that are type decimal. I'm writing th following code:

List<string> values (this is the list with all the string values, they come from several inputs form the user)

then im using my class "Promedio" to create a new object and trying to set the values of the List to the fields of my class, like this:

Promedios promedio = new()
{
C1 = Convert.ToDecimal(values[0]),
C2 = Convert.ToDecimal(values[1]),
Leaf = Convert.ToDecimal(values[2]),
Stpl = Convert.ToDecimal(values[3]),
Mic = Convert.ToDecimal(values[4])
}

but instead of returning decimal values, it returns items like this:

values[0] = "1.2367"

and C1 ends up being: 12376

I tried several solutions but I dont know what else to do, please help

guillermomazzari
Автор

very helpful and also i liked try and catch :) many thanks

mertkurt
Автор

Always interesting as usual... But what happened to your voice???? Where is the Castafiore??? ;)

PhilippeLhermie
Автор

How to create a desktop clock(widget) in C# for window 10? Can you make a tutorial on that please?

adammichael
Автор

Why the hell would you put background music on this type of video? Jfc!

bojohannesen