Start learning free

LESSON 7 OF 347 · WELCOME TO C#

Strings vs Numbers in C#: How the Compiler Tells Them Apart

Lesson Read the full explanation

  • C# distinguishes between numbers and strings!


  • In C#, "15" (with quotes) is considered a string, while 15 (without quotes) is considered a number.


  • For example, computers can perform mathematical operations on numbers, but not on strings. More on this in the next lessons.

STEP 1 OF 1
    Ctrl + Enter to check

    C# · WELCOME TO C#

    Strings Vs Numbers in C#, explained

    Quotes make the difference

    In C#, a value with double quotes around it is a string, which is text. A value without quotes that looks like a number is a number. So "15" and 15 are different things, even though they look alike on the screen:

    string text = "15";   // text made of the characters 1 and 5
    int number = 15;      // the number fifteen
    

    A string can hold anything: letters, digits, spaces, punctuation. C# does not look inside it to see whether it resembles a number. Whatever sits between the quotes is just characters.

    Every variable has a declared type

    When you declare a variable, you write its type first. int holds whole numbers and string holds text. The compiler then checks every assignment against that type, and refuses to build a program where they disagree:

    int age = "29";
    

    This fails with Cannot implicitly convert type 'string' to 'int'. The reverse is rejected too:

    string phone = 2025550153;
    

    Here the message is Cannot implicitly convert type 'int' to 'string'. In both cases the error appears at compile time, before the program starts. That is a benefit: a type mismatch can't slip into a running program.

    Why the difference matters

    Numbers support arithmetic. Strings do not:

    Console.WriteLine(15 + 1);     // 16
    Console.WriteLine("15" + 1);   // 151
    

    With two numbers, + adds. With a string on either side, + joins the pieces of text, and the number is turned into text first. So "15" + 1 gives the string 151, not 16. Other operators don't even have a meaning for text: multiplying "peanut" * "butter" is a compile error. The next lessons cover these operations in detail.

    Which one should it be?

    Ask whether you will ever calculate with the value. An age, a price or a count is a quantity, so it is a number. A phone number, a postal code or an ID is an identifier. Adding two phone numbers together has no meaning, and these often contain leading zeros, plus signs or dashes, which a number can't keep. Store them as strings.

    Numbers have types too

    int is only one kind of number. A value with a decimal point, such as 9.99, is a double, and C# won't squeeze it into an int variable for you: int price = 9.99; is an error that suggests a cast. The lesson on data types lists the main ones, and the one on type conversion shows how to change a value from one type to another on purpose.

    The exercise, step by step

    The starter code has both declarations the wrong way round:

    int age = "29";
    
    string phone = 2025550153;
    
    1. Make age a number. Remove the quotes: int age = 29;. An age is something you may calculate with, so int fits.
    2. Make phone a string. Add quotes: string phone = "2025550153";. The digits become text, which is the right choice for an identifier.

    The corrected code:

    int age = 29;
    
    string phone = "2025550153";
    

    The types stay as written and the values change, because the declared type states what the variable is meant to hold. Changing the value to match is the fix that keeps the intent.

    Common mistakes

    • Quoting a number you want to calculate with. "29" is text, so arithmetic on it doesn't work. Converting text to numbers is a later topic.
    • Single quotes for strings. '29' is a char literal, one character, and a two-character one is a compile error. Strings use double quotes.
    • Large numbers. An int tops out at 2,147,483,647. A ten-digit phone number may not fit, which is another reason to keep it as a string.

    Key takeaways

    • Double quotes make a string; no quotes makes a number.
    • The compiler rejects a value whose type does not match the variable's declared type.
    • Store quantities you calculate with as numbers and identifiers as strings.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free