C# Casting
int count = (int) 40.0; stores the whole number 40, and Console.WriteLine(count) writes 40. The screen is one line. The point is not on that line, and the 0 after the point is not on that line. Keep the public class named Pine.
The kind word does not match the digits
Open the Pine file. The class name stays Pine. This page does not rename the type. Delete bool ripe if that line is still there, including true and false. Delete string plant if it is still there. Delete plant.Length and every line that asks a question after a dot. Delete any ReadLine line. Delete int count = 40; if that line has no point and no parentheses. The file you keep has no wait, no plant, no ripe, and no word true. The number is written only by the writing line that uses the name.
The numbers page already showed the mismatch. int count = 40.0; asks an int name to hold a number that is written with a point. The build writes an error. It does not stay quiet. It does not drop the point for you. There is no screen to read while that error is being written. This page does not leave the line like that. The kind word int does not match 40.0. You write that same kind word again, inside parentheses, between the = sign and the digits.
Inside Main, the storing line is int count = (int) 40.0;. int, the first one, is the kind word for the name. It says count holds a whole number. count is the name, and it starts with a small c. The = sign stores. Then (int) is the kind word again, alone inside one pair of parentheses. There is no digit inside those parentheses. The value sits outside, after the closing parenthesis: 40.0. The 4 and the 0 before the point are the whole number. The point says there is a further digit. That further digit is 0. The semicolon ends the line. This line stores 40. It does not write the screen. Storing is not writing. The screen stays blank through this line. The parentheses are not a second number. They are not quotes. They are not a question after a name. There is no plant on this page, so the point in 40.0 is not a dot that asks Length.
The writing line is the bare name: Console.WriteLine(count);. No quotes, and no parentheses around count. C# reads the name count, writes the whole number that name holds, and ends the line. For the file you keep, that number is 40. The letters of the name are not on the screen. int is not on the screen. The parentheses are not on the screen. The point is not on the screen. The 0 that stood after the point is not on the screen. The screen is 40, one line. Save the file.
The file you keep
Keep this teaching shape. One name, count. One kind word for that name, int. One pair of parentheses, and inside them only int. One value, 40.0, outside the parentheses. One writing line, and that line uses the bare name.
using System;
public class Pine
{
public static void Main()
{
int count = (int) 40.0;
Console.WriteLine(count);
}
}
Read it from the top. using System; prepares short names. public class Pine names the type. public static void Main() is where the runtime starts. There is no string plant line, and there is no bool line. int count = (int) 40.0; stores 40. It writes nothing. Console.WriteLine(count); writes 40 and ends the line. One line on the screen. No point. No second line.
Run it with dotnet run. The program writes 40. It does not wait for you to type. The digits were already in the file. If dotnet run waits, ReadLine is still in the file. Delete that line and keep int count = (int) 40.0; and the one writing line. The program finishes after the writing line. The screen shows 40. One line. No point. If the screen shows 40.0, the writing line is printing a number that still has a point, or the parentheses are not on the storing line you ran. The file you keep prints count, and count holds a whole number. If the screen shows 40.9, you are printing the dotted number, not the whole number after the parentheses. If the screen shows 41, the file holds 41. These parentheses do not round 40.9 up to 41. If the screen shows true or false, the boolean file is still in place. Delete ripe. If the screen shows leaf, plant is still in the file. If the screen shows count, the writing line has quotes around the name. You do not reinstall the .NET SDK for a screen that shows the wrong line.
A screen that shows 40 does not, by itself, prove the parentheses. int count = 40; with no point and no parentheses also writes 40. The screen can look the same. The file does not. The file you keep has (int) and it has 40.0. When you judge this page, look for the parentheses and look for the point in the file. Do not judge the parentheses from the screen alone.
Without the parentheses, the mismatch is still an error. int count = 40.0; makes the build write an error. It does not write 40. It does not stay quiet. You do not run the program while the build is writing an error. You do not need the exact words of that error. Put (int) between = and 40.0 so the line is int count = (int) 40.0; again. The value does not go inside the parentheses. int count = (int 40.0); makes the build write an error. The kind word sits alone inside. The digits sit outside, after the closing parenthesis.
Quotes and the word true do not fit inside this pair of parentheses. int count = (int) "40"; asks the parentheses to turn letters into a whole number. The build writes an error. int count = (int) true; asks them to turn the word true into a whole number. The build writes an error. It does not write 1. bool ripe = (bool) 1; asks the other kind to turn a digit into true. The build writes an error. It does not write true. There is no screen to read for any of these, because you do not run while the build is writing an error. Delete the quotes, delete true, and delete the boolean line when you are repairing the file you keep.
The name has to exist before a line uses it. If Console.WriteLine(count); is written above int count = (int) 40.0;, the build writes an error. Move the storing line above the writing line. Do not write int a second time for the same name. A second int count under a line that already created count is a different error. The second file on this page replaces the storing line. It does not add another int count under the first. int count = (int); with nothing after the parentheses makes the build write an error. The value has to sit there. A space inside the parentheses, int count = ( int ) 40.0;, still stores 40, and the build writes no error for that alone. The file you keep has no space inside the parentheses. No space after the closing parenthesis, int count = (int)40.0;, also stores 40. The file you keep has one space there. Neither extra space nor a missing space is a second number.
Digits after the point are dropped
40.9 is not stored as 41, and it is not stored as 40.9, when the parentheses hold int. int count = (int) 40.9; stores 40. The 9 is dropped. It is not rounded up. It is not a second number on the line. It is not a second line on the screen. The writing line is still Console.WriteLine(count);. The screen shows 40. One line. No point. The name count is not on the screen.
This is the second file. It is not the file you keep. The file you keep uses 40.0, and the digit after the point is 0. Save this second shape as the program to see that a 9 is dropped, then put int count = (int) 40.0; back.
using System;
public class Pine
{
public static void Main()
{
int count = (int) 40.9;
Console.WriteLine(count);
}
}
Run dotnet run. The screen shows 40. One line. The 9 does not make the build fail. If the screen shows 41, this file is not the file you ran. If the screen shows 40.9, the parentheses are missing, or the writing line is printing the dotted number itself. If the screen shows 40.0, you are still on the file you keep, which is the right file to put back, but it is not this check. After you have seen 40 from 40.9, put the kept file back: int count = (int) 40.0; and one Console.WriteLine(count);.
The parentheses can sit in front of a name
A name can hold the dotted number first. double is the kind word that matches a point. double weight = 40.9; stores 40.9 in weight. That line has no parentheses, because the kind word double already matches a value written with a point. The next line is the one that does not match: int count = (int) weight;. The parentheses hold int, the kind count can hold. They sit in front of the name weight, not in front of fresh digits. weight already holds 40.9. The parentheses drop the 9, and count holds 40. The writing line is still Console.WriteLine(count);. The screen shows 40. weight is not on the screen. double is not on the screen. 40.9 is not on the screen.
This is the third file. It is not the file you keep. The file you keep has no double and no weight. It writes the digits 40.0 on the storing line. The word inside the parentheses has to be a kind the name can hold. int count = (double) 40.0; puts double inside the parentheses and int on the name. Those two kind words do not match. The build writes an error. It does not write 40.0. It does not stay quiet.
using System;
public class Pine
{
public static void Main()
{
double weight = 40.9;
int count = (int) weight;
Console.WriteLine(count);
}
}
Run dotnet run. The screen shows 40. One line. double weight = 40.9; does not need parentheses, and it does not make the build fail. If the screen shows 40.9, the writing line is Console.WriteLine(weight);. Change it back to Console.WriteLine(count);. If the screen shows weight, the writing line has quotes around that name. After you have seen 40, put the kept file back. Do not leave double in the file you keep.
You can now store a whole number from a value that was written with a point. int count = (int) 40.0; stores 40. Console.WriteLine(count); writes 40 and ends the line. The point is not on the screen. int count = (int) 40.9; also writes 40. The 9 is dropped, not rounded. The same parentheses can sit in front of a name that already holds 40.9, and the screen is still 40. Without the parentheses, int count = 40.0; makes the build write an error. (int) true makes the build write an error. The next page leaves these parentheses. It looks at a name that holds no number you wrote and no word you wrote.
The screen is the whole number that fits
The file you keep writes 40. The name on the writing line is count, and count is not on the screen. The parentheses are in the file, and they are not on the screen. One WriteLine is one line. A 9 after the point is dropped. A name that holds 40.9 can stand inside the same parentheses, and the screen is still the whole number.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top