Loading...

C# Value Types

int count = 4; stores the whole number 4 in the file, and Console.WriteLine(count) writes 4. No quotes sit around 4. The word int in front of the name says the value is a whole number. The previous page filled plant from the keyboard, so those letters were not written in the file. Keep the public class named Pine. You are not installing the SDK again.

The kind word sits in front of the name

Open the Pine file. The class name stays Pine. This page does not rename the type. The last page waited on Console.ReadLine(). This page does not. Delete the ReadLine line. The file you keep has no wait and no typed line. An unused keyboard call is not what writes the number. The number is written only by the writing line that uses the number's name.

Inside Main, the storing line is int count = 4;. int is the kind word. It says this name holds a whole number: digits, and no decimal point. count is the name, and it starts with a small c. The = sign stores the number. The 4 has no quotes around it. A whole number in this file is not a word between quotes. The semicolon ends the line. This line does not write the screen. Storing is not writing. The screen stays blank through this line.

The writing line is the bare name: Console.WriteLine(count);. No quotes. C# reads the name count, writes the whole number that name is holding, and ends the line. For the file you keep, that number is 4. The letters of the name, c, o, u, n, t, are not on the screen. int is not on the screen. Console is not on the screen. The semicolon is not on the screen. Save the file.

The file you keep

Keep this teaching shape. One kind word, int. One name, count. One whole number, 4, with no quotes and no decimal point. One writing line, and that line uses the bare name.

the file you keep

csharp
using System;

public class Pine
{
    public static void Main()
    {
        int count = 4;
        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. None of those words are the number on the screen. int count = 4; stores the whole number. It writes nothing. Console.WriteLine(count); is the only writing line. It writes 4 and ends the line. One line. No quotes.

Run it with dotnet run. The program writes 4. It does not wait for you to type. The number was already in the file. If dotnet run waits, the file still has ReadLine in it. Delete that line and keep int count = 4;. The screen shows 4, one line, no quotes. If the screen shows count, the writing line has quotes around the name: Console.WriteLine("count");. Those quotes mark the letters c, o, u, n, t. C# does not look up the name, so the stored 4 is not written. The file you keep has the bare name. You do not reinstall the .NET SDK for a screen that shows the wrong line.

A screen that shows 4 does not, by itself, prove the kind. string count = "4"; would also make Console.WriteLine(count); write 4, because the letters between the quotes are the digit 4. The screen looks the same. The file does not. The file you keep has int, and it has 4 with no quotes. string count = "4"; is the letter kind you already know, with a digit written as letters. This page's kept file is the whole-number kind. When you judge the file, look for the kind word and look for quotes. Do not judge the kind from the screen alone.

int and string store differently

Keep the two kinds easy to tell apart on the storing line.

📊 Value type habits for count and plant

Line Kind Quotes
int count = 4; whole number none around 4
string plant = "leaf"; letters required around leaf
int count = "leaf"; mismatch build writes an error
count = 9; same int name again none around 9

Change the number, keep the writing line

The writing line can stay put while the stored number changes. Under the first writing line, store a new whole number in the same name, then write the name again. The new storing line is count = 9;. It does not start with int. count already exists, so this line only replaces the number the name holds. 4 is replaced by 9. The next writing line is again Console.WriteLine(count);. The screen now has two lines. The first is 4, from the number the name held at that moment. The second is 9, from the number the name holds after count = 9;.

This is the second file. It is not the file you keep. The file you keep has one storing line and one writing line, and its screen is 4. Use this second shape when you want to see the number change, then put the single storing line and the single writing line back.

the number changes between two lines

csharp
using System;

public class Pine
{
    public static void Main()
    {
        int count = 4;
        Console.WriteLine(count);
        count = 9;
        Console.WriteLine(count);
    }
}

Run dotnet run. The screen shows 4, and the next line is 9. Two lines. No quotes. The name count is on neither line. If both lines say 4, the second storing line is missing or still says 4. If both say 9, the first storing line already says 9, or the first writing line sits below the change. The screen follows the order of the lines in the file. Do not write int a second time for the same name. int count = 9;, placed under a line that already created count, is a different mistake. The build writes an error. Delete the second int and leave count = 9;.

Letters do not fit a whole number

int means a whole number. A word between quotes is letters, the kind you stored before the keyboard page. int count = "leaf"; asks an int name to hold letters. Those do not fit. The build writes an error. It does not stay quiet. There is no screen to trust while the build is writing an error. The repair is to store a whole number with no quotes, int count = 4;, or to change the kind word if you meant letters. Changing the kind word to string makes string count = "leaf";, which is the letter kind, not the file you keep. This page keeps int and keeps 4.

Quotes around the digits are the same mismatch. int count = "4"; still has quotes, so the 4 is letters, one digit long. The build writes an error. It does not turn those letters into the whole number 4. Delete the quotes if the kind word is int. A decimal point is also not a whole number. int count = 4.5; makes the build write an error. The file you keep has 4, with no point and no digit after a point.

This third file is the letter mismatch. It is not the file you keep. Keep it only until the build writes the error, then put int count = 4; back.

letters stored in a whole-number name

csharp
using System;

public class Pine
{
    public static void Main()
    {
        int count = "leaf";
        Console.WriteLine(count);
    }
}

Two kinds in one file

A file can hold a letter name and a whole-number name together. The kind word on each storing line is what makes them different. string plant = "leaf"; stores letters, and the quotes are required around leaf. int count = 4; stores a whole number, and there are no quotes around 4. Two writing lines follow, one for each name. The first writes leaf. The second writes 4. Two screen lines. The names plant and count are not on the screen.

This is the fourth file. It is not the file you keep. The file you keep has only the whole number, and its screen is 4 alone. Use this fourth shape when you want to see the two kinds, then put the single int line and the single writing line back.

a letter name beside a whole-number name

csharp
using System;

public class Pine
{
    public static void Main()
    {
        string plant = "leaf";
        int count = 4;
        Console.WriteLine(plant);
        Console.WriteLine(count);
    }
}

The screen is the number the name holds

You can now store a whole number in a name and write that number without quotes and without the keyboard. int count = 4; stores 4. Console.WriteLine(count); writes 4 and ends the line. A later count = 9; changes the stored number without a second int. Letters, a quoted digit, and a decimal do not fit int, and the build writes an error for each of them. A string name can sit beside the int name, and each writing line shows its own kind. The next page stays with letters. It looks at the string kind more closely than the one line leaf you already know how to store.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
6
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 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