No history yet

Variables and Data Types

Transcript

Beau

Okay, so last time we got the computer to print 'Hello, World!'. Which, don't get me wrong, felt like magic. But it's a pretty limited magic trick, you know? How do we get it to do something... more? Like, remember my name or a number I give it?

Jo

That is the perfect next step. We need to give the computer a memory. And in programming, we do that with something called variables.

Beau

Variables. Okay, sounds... math-y. Like algebra class, x equals five.

Jo

It's a fair comparison, but I find it's easier to think of them as labeled boxes. You take a box, you slap a label on it, say... 'playerScore', and then you put something inside it. Like the number zero.

Beau

A labeled box. I like that. So I can have a box labeled 'playerName' and put 'Beau' inside it?

Jo

Exactly. But C# is a bit particular. It wants to know what *kind* of thing you plan to put in the box before you do. It's... let's call it a 'strongly-typed' language.

Beau

Strongly-typed... So I can't just say 'Here's a box called playerScore'. I have to say 'Here's a box for numbers, and I'm calling it playerScore'?

Jo

Precisely. And that 'type' for a whole number is `int`, short for integer. So you'd write: `int playerScore = 0;`. You're declaring the type, naming the variable, and initializing it with a value all at once.

Beau

Okay, `int playerScore = 0;`. The semicolon at the end, that's like a period on a sentence, right? Tells C# we're done with that thought.

Jo

Yep, you got it. So, what about your name? What type of box would that need?

Beau

Well it's not a number... it's... text? A word?

Jo

In programming, we call a sequence of characters like that a 'string'. So you would write `string playerName = "Beau";`. Notice the double quotes around your name.

Beau

Right, quotes for text, no quotes for numbers. Got it. So we have `int` for integers, `string` for text. What else is there? What if I wanted to store, like, the price of an item? 1.99.

Jo

Good question. An `int` can only be a whole number. For numbers with decimal points, you'd typically use a `float`. For... floating-point number. You'd write `float itemPrice = 1.99f;`.

Beau

Wait, what's that little 'f' at the end for?

Jo

Ah, that's a C# thing. It's you explicitly telling the compiler, 'Hey, trust me, this is a float.' There are other types for decimal numbers, like `double` and `decimal`, which are more precise, but `float` is super common, especially in game development.

Beau

Okay, so `int`, `string`, `float`... feels like I can store most things with those. Is there one for just... a yes or no answer? Like, is the game over?

Jo

Absolutely. That's called a `bool`, short for Boolean. It can only hold two possible values: `true` or `false`. So you'd write `bool isGameOver = false;`. Extremely useful.

Beau

I can see that. So these... `int`, `float`, `bool`... they all seem pretty simple. Just a single value.

Jo

They are. We call those 'value types'. When you create a variable of a value type, the box you labeled literally contains the value. If you have `int score = 100`, that box labeled 'score' has the number 100 right inside it.

Beau

Okay... that sounds obvious. Is there another way?

Jo

There is. It's called a 'reference type'. `string` is actually a reference type, even though it sometimes acts like a value type. With a reference type, the box doesn't hold the thing itself. It holds an address—a piece of paper that tells you where to find the actual thing in memory.

Beau

Whoa, okay. So my `int` box has a 100-dollar bill in it. My `string` box has a note that says 'The money is in the cookie jar'.

Jo

That's a fantastic analogy. And it matters. Imagine you have a box, `scoreA`, with 100 in it. You make a copy: `scoreB = scoreA`. Now you have two separate boxes, both with 100 inside. If you change `scoreB` to 50, `scoreA` is still 100.

Beau

Makes sense. Two separate hundred-dollar bills.

Jo

Right. But now imagine you have a reference type, like a list of players. Let's say `teamA` is a box with a note that says 'The list is in the cookie jar.' If you make a copy, `teamB = teamA`, you're not copying the list. You're just copying the note.

Beau

So now I have two notes, but both point to the *same* cookie jar.

Jo

Exactly! So if you use the `teamB` note to go to the cookie jar and cross a player's name off the list, you've also changed it for `teamA`, because they both point to that one single list.

Beau

Okay, that's a huge difference. And a little scary. I can see how you could mess things up by accident that way.

Jo

It's one of those fundamental concepts that, once you get it, saves you from hours of debugging later on. Value types hold the data. Reference types hold a pointer to the data.

Beau

So since C# is 'strongly-typed', what happens if you try to put the wrong thing in a box? Like, I have my `int playerScore = 0;` and then later I try to do `playerScore = "Beau";`?

Jo

It won't even let you run the program. The compiler will give you an error immediately. It's like a bouncer at a club checking IDs. It says, 'Nope, this box is for integers only. The string 'Beau' can't come in.' That's the 'strong' part of strongly-typed. It protects you from yourself.

Beau

But what if I have a number that's stored as text? Like, I get user input and it comes in as the string "100". Can I put that in my `int playerScore` box?

Jo

Not directly, but you can convert it. It's called 'type conversion' or 'casting'. You're essentially asking C# to try and change the type. So you can take the string "100" and say, 'Please try to parse this as an integer.' If it succeeds, you get the number 100. If it fails—like if the string was "hello"—it'll cause an error.

Beau

So you can change between types, but you have to be explicit about it. You can't just shove a `float` into an `int` box without telling C# what to do with the decimal part.

Jo

Exactly. If you have `float playerHealth = 99.5f;` and you try to put it into an `int` variable, you have to cast it. You'd say `(int)playerHealth`. And C# will just chop off the decimal, leaving you with 99. You might lose information, so you have to be careful, but you're telling the compiler, 'I know what I'm doing.'

Beau

Okay, I think I'm getting it. It's all about creating labeled boxes, being very specific about what can go in them, and then carefully managing what's inside—whether it's the thing itself or just directions to find it.

Jo

That's the core of it. Everything else we build, all the logic and cool features, it's all built on top of moving data in and out of these little boxes.