Focus video player for keyboard shortcuts
Auto
- HD
- 720p
- 540p
- 360p
1.00x
cc
- 0.50x
- 0.75x
- 1.00x
- 1.25x
- 1.50x
- 1.75x
- 2.00x
We hope you enjoyed this lesson.
Cool lesson, huh? Share it with your friends
About this lesson
In this lesson, we discuss how to convert Integers to Strings using the toString method.
Exercise files
Download this lesson’s related exercise files.
Converting Integers To Strings61.1 KB Converting Integers To Strings - Solution
58.4 KB
Quick reference
Converting Integers To Strings
It's easy to convert an integer to a string using the .toString() method.
When to use
Use this method whenver you need to convert an integer to a string.
Instructions
int x = 41;
string y;
y = x.ToString();
- 00:04 Okay, in this video I want to talk about converting integers to strings.
- 00:07 Now, there are many times when you might have an integer and
- 00:10 you need to convert it to a string for any number of reasons.
- 00:12 Not going to get into why you might want to do that in this video, but
- 00:15 there are times when you want to do that.
- 00:17 How can we do that?
- 00:18 So let's start out with an integer.
- 00:20 I'm going to create an integer, let's just call it x, and set it equal to 41.
- 00:24 Now, let's also create a string, let's call it y, and
- 00:28 maybe we haven't assigned anything to it yet.
- 00:31 So let's say we want to put whatever is in our x variable into this y string, and
- 00:36 use it as a string, how do we do that?
- 00:39 Well, you might think you just might want to go y = x, right?
- 00:43 So then if we come down here and just try to print out y, and then run this,
- 00:47 we get an error.
- 00:49 And when we click on this thing to see, it says,
- 00:52 you cannot implicitly convert type int to string.
- 00:55 So a lot of programming languages will let you just do something like this, and
- 01:00 it will convert it for you.
- 01:01 Not so with C#, we have to be very specific.
- 01:04 But there's an actual function that we can use that will convert
- 01:06 something to a string.
- 01:08 And we can use that, and it is the .toString() function.
- 01:14 So here, we can set our y, which we've already declared as a string.
- 01:18 And instead of just calling this y = x,
- 01:22 we can call y = x.toString(),
- 01:26 like that, and then now we can print this out.
- 01:31 So let's go ahead and run this guy, and when we do, we see 41.
- 01:35 Now, is that an integer?
- 01:37 Is it a string?
- 01:38 We can't really tell by looking at it.
- 01:40 It looks like an integer.
- 01:42 But we know, if we concatenate a string with another number,
- 01:47 say 10, it will not add, right?
- 01:50 Strings can't add.
- 01:51 It will just smoosh them together.
- 01:53 We've seen that in prior videos, right?
- 01:54 So, if this has actually converted it to a string,
- 01:58 it should print out something like 4110,
- 02:02 which is just 41, which is our guy right here,
- 02:06 smooshed together with this 10, also treated as a string.
- 02:11 And yeah, that's all there is to it.
- 02:13 So let's go ahead and save this and run it, see if that worked.
- 02:17 Sure enough 4110, all smooshed together, definitely not added.
- 02:22 Now, if we undid this, and changed this back to x,
- 02:27 now this would add 41 and 10, right?
- 02:32 And we should just get 51, which we do.
- 02:36 So that's what happens when they're integers.
- 02:41 When they're not integers, for instance, when we convert it to a string,
- 02:44 it'll be as we just saw, smooshed together, not added at all.
- 02:48 And in fact, that's what we get.
- 02:49 So that's how you can convert integers to strings.
- 02:52 Very handy to use this all the time for all kinds of weird things, and
- 02:55 pretty simple with this .toString() function.
- 02:58 So, that's all for this video.
- 02:59 In the next video, we'll dive into this a little bit deeper and
- 03:02 talk about type conversion and casting.
Lesson notes are only available for subscribers.