Start learning free

LESSON 10 OF 347 · VARIABLES

Data Types in C#: int, double, string, bool and char

Lesson Read the full explanation

TL ; DR:

In C#, not all data is treated the same. There are different data types, such as:

  • int - represents integer numbers like 3 and -12

  • double - represents floating point numbers like 3.14

  • string - represents a string (sequence of characters) like "John Doe"

  • bool - represents data that can have one of two values: true or false

  • char - represents a single character like 'a', 'Z' or '?'



Full lesson:

One instruction can have several distinct meanings depending on the data types it's working with.

For example, suppose you see this line of code somewhere in a program:

Console.WriteLine(a + b);

Can you tell for sure what it does without knowing anything about the variables a and b?

It might add two numbers, if a and b are both numbers:

int a = 10;
int b = -5;

Console.WriteLine(a + b); // Output: 5

But it also might concatenate two strings, if a and b are both strings:

string a = "Hello ";
string b = "world!";

Console.WriteLine(a + b); // Output: Hello world!

Of course, addition and string concatenation are two completely different operations and C# needs to figure out which one to do.

And this is exactly why C# assesses the data type of every value you use in your program. In order to be able to make these decisions when needed.

In our example, when it reads a + b, C# checks the data type of a and b. If both are strings, it performs string concatenation. If both are numbers, it performs addition.


Invalid operations:

Asessing data types is also helpful for C# to make sure that what you do with the data actually makes sense.

For example, computers can perform mathematical operations on numbers, but not on strings. Of course, nobody can stop you from writing a program like this:

string a = "peanut";
string b = "butter";

Console.WriteLine(a * b); // TypeError

But when you try to run this program, C# will throw a TypeError: bad operand types for binary operator '*'.

This is the way of C# telling you: "Hey, I'm a little confused. How do you multiply two strings? I've never heard of that."


Assignment
Follow the Coding Tutorial and let's practice with data types!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 4
    Ctrl + Enter to check

    C# · VARIABLES

    Data Types in C#, explained

    What a data type is

    C# does not treat all data alike. Every value and every variable has a data type that says what kind of data it is and what you can do with it. You write the type in front of the variable's name when you declare it. The five types you will use first are:

    TypeHoldsExample values
    intwhole numbers3, -12
    doublefloating-point numbers, with a decimal part3.14, -0.5
    stringtext, a sequence of characters"John Doe"
    boolone of two valuestrue, false
    chara single character'a', 'Z', '?'

    Note the quotes: strings use double quotes and char values use single quotes. true and false are written in lowercase and without quotes.

    C# has more types. long holds larger whole numbers than int (an int stops at 2,147,483,647), float is a smaller floating-point type, and decimal is meant for exact decimal arithmetic. Start with the five above.

    Why C# cares about types

    The same symbol can mean different operations depending on the types. Take this line:

    Console.WriteLine(a + b);
    

    Without knowing the types of a and b, you can't tell what it does. With two integers it adds:

    int a = 10;
    int b = -5;
    
    Console.WriteLine(a + b);   // 5
    

    With two strings it concatenates:

    string a = "Hello ";
    string b = "world!";
    
    Console.WriteLine(a + b);   // Hello world!
    

    Addition and joining text are different operations, and the compiler decides which one to perform from the types of the operands. That decision is made before the program runs.

    Seeing a value's type

    Every value can report its type with GetType(). The names you see are the full .NET names behind the keywords:

    int count = 5;
    double ratio = 3.14;
    string title = "Hello";
    Console.WriteLine(count.GetType());   // System.Int32
    Console.WriteLine(ratio.GetType());   // System.Double
    Console.WriteLine(title.GetType());   // System.String
    

    So int, double and string are short names for System.Int32, System.Double and System.String.

    char is not a one-letter string

    A char is a single character and behaves like a small number underneath. 'a' and "a" are different types, and adding to a char gives an int:

    char c = 'a';
    Console.WriteLine(c + 1);         // 98
    Console.WriteLine((char)(c + 1)); // b
    

    Invalid operations are caught early

    Types also protect you from nonsense. Numbers can be multiplied, strings can't:

    string a = "peanut";
    string b = "butter";
    
    Console.WriteLine(a * b);
    

    C# refuses to compile this and reports Operator '*' cannot be applied to operands of type 'string' and 'string'. Some languages report this kind of mismatch while the program runs, as a TypeError. In C# it is a compile-time error, and the program never starts.

    The same checking applies to assignments. A variable keeps its declared type for its whole life, so int count = "five"; is rejected, and so is int price = 9.99;, because a double can't silently become an int.

    Letting the compiler infer the type

    You can write var in place of the type when you give an initial value, as in var count = 5;. The compiler infers int and it stays int. The variable is still strongly typed, and this lesson uses explicit types because the type is the topic.

    The exercise, step by step

    You build four variables, one per step, and keep the earlier ones.

    1. An int named myInt. Give it any whole number. The reference solution uses 5.
    2. A double named myFloat. Give it a number with a decimal point, such as 3.14. The name says "float", but the type the lesson asks for is double, C#'s default type for decimal literals.
    3. A string named myString. Give it text in double quotes, such as "Hello".
    4. A bool named myBool. Give it true or false.
    int myInt = 5;
    double myFloat = 3.14;
    string myString = "Hello";
    bool myBool = true;
    

    Each line follows the same pattern: a type, a name, =, a value of that type, and a semicolon. The order of the four lines matters only because the steps ask for them on lines 1 to 4, and none of them uses another.

    Common mistakes

    • Using the wrong quotes. 'Hello' is not a string. Single quotes are for exactly one character.
    • Capitalising booleans. True is not true; C# is case-sensitive.
    • Putting a decimal value in an int. Use double for values with a decimal part.
    • Forgetting the type. myInt = 5; without a declaration reports an unknown name.

    Key takeaways

    • Every value has a type, and the type decides what operations mean.
    • The basic types are int, double, string, bool and char.
    • C# checks types at compile time, so invalid operations and mismatched assignments never run.
    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