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:
| Type | Holds | Example values |
|---|---|---|
int | whole numbers | 3, -12 |
double | floating-point numbers, with a decimal part | 3.14, -0.5 |
string | text, a sequence of characters | "John Doe" |
bool | one of two values | true, false |
char | a 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.
- An
intnamedmyInt. Give it any whole number. The reference solution uses5. - A
doublenamedmyFloat. Give it a number with a decimal point, such as3.14. The name says "float", but the type the lesson asks for isdouble, C#'s default type for decimal literals. - A
stringnamedmyString. Give it text in double quotes, such as"Hello". - A
boolnamedmyBool. Give ittrueorfalse.
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.
Trueis nottrue; C# is case-sensitive. - Putting a decimal value in an
int. Usedoublefor 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,boolandchar. - C# checks types at compile time, so invalid operations and mismatched assignments never run.