Why two ways of converting?

357mag

Well-known member
Joined
Mar 31, 2023
Messages
58
Programming Experience
3-5
I'm kinda confused about converting a string to an int. It seems like there is at least two ways to do it that I've discovered.
C#:
test1 = Convert.ToInt32(textBoxScore1.Text);
test2 = int.Parse(textBoxScore2.Text);

Is it just because two of something is better than one?
 
Here is the implementation of that Convert.ToInt32:
C#:
public static int ToInt32(String value) {
    if (value == null)
        return 0;
    return Int32.Parse(value, CultureInfo.CurrentCulture);
}
As you can see, it calls that int.Parse method but it calls it in a specific way. The Convert class contains numerous methods that convert between various types in a way that most people will want most of the time. If you want something different, you can drop down to a lower level and call int.Parse in the specific manner you want.
 
Back
Top Bottom