Member-only story
7 Things about C#: Types

A type is a name given to some container that holds data of a specific size and shape. e.g. an int
is a type that holds an integer number, a char
is a type that holds a symbolic character, and a Recipe
might be a complex type that holds ingredients and instructions.
With the C# type system, the big win for you is in productivity for your coding experience. C# is referred to as a strongly typed or type safe language. This means that you can only assign a value to a compatibly typed variable. It’s productive because the compiler catches type safety violations as soon as you build the app. That way, you can find and fix an entire category of errors in your app right away. The alternative would be to not find those errors and need to deal with them some time in the future where those errors might be even more problematic, difficult to detect, and expensive to correct.
Note: On type safety, the C# compiler will emit an error and not compile the app on type errors. e.g. trying to assign incompatible types, like a
string
to anint
. These are called compile-time errors. Another type of error that can occur is a run-time error that the compiler wasn’t able to detect, which occurs when the application is running.
1 — There are several built-in types
C# has a few categories of built-in types including: integrals, floating point, other primitive types, and reference types. This section is a brief overview and we’ll dive deeper into some of the more complex subjects in other parts of this series.
You’ve seen examples of how to declare a variable with a type, in this format:
<type> <variable name> = <value>;
Here’s an example:
bool isCSharpCool = true;
Here, the <type>
is bool
, the <variable name>
is isCSharpCool
, and the <value>
is true
. Because C# is strongly typed, you can only pass a bool
type value to a bool
type variable. In the case of the above example, isCSharpCool
can only be true
or false
. Setting it to a number, string, or other object would result in a compile-time error.
As you’re going through each of these sections, take note of the Range
of each type (where applicable). They help you decide which type to use.