Strong Typing and Type Inference

How C# helps you avoid mistakes

Posted by Rodrigo Castro on October 13, 2024

C# is a strongly typed language. This means the compiler checks that you’re using the right types in your code, making it safer and easier to debug.

🔒 What is Strong Typing?

  • Variables must have a type: int, string, bool, etc.
  • You can’t accidentally assign a string to an int:
    1
    
      int age = "twenty"; // ❌ Error!
    

Benefits:

  • Catches mistakes early (before you run your program).
  • Helps tools (like IntelliSense) suggest code and catch bugs.

🔍 Type Inference with var

C# can figure out the type for you when you use var:

1
2
var name = "Alice";    // C# knows this is a string
var count = 10;        // C# knows this is an int
  • var is for local variables only.
  • The type is still checked by the compiler!

Good practice: Use var when the type is clear from the right side.

🛡️ Why It Matters

  • Strong typing makes code predictable and readable.
  • Type inference saves typing, but keeps the benefits of strong typing.

💡 Try It!

What type does each variable get?

1
2
var temperature = 32.5;
var isSunny = true;

Answers:

  • temperature is a double
  • isSunny is a bool

Built-In Primitive Types in C#

C# offers a set of built-in types, each with a keyword and a corresponding .NET type. Here’s a handy reference:

C# Keyword .NET Type Category Example Value Notes
bool Boolean Value true, false  
byte Byte Value 255 Unsigned 8-bit
sbyte SByte Value -128 Signed 8-bit
char Char Value 'A' Unicode character
decimal Decimal Value 10.5m High-precision
double Double Value 3.14 64-bit float
float Single Value 3.14f 32-bit float
int Int32 Value 42 Signed 32-bit
uint UInt32 Value 42u Unsigned 32-bit
long Int64 Value 42L Signed 64-bit
ulong UInt64 Value 42UL Unsigned 64-bit
short Int16 Value -32768 Signed 16-bit
ushort UInt16 Value 65535 Unsigned 16-bit
object Object Reference   Base of all types
string String Reference "hello" Sequence of chars

Tip: All value types are derived from System.ValueType, and all types in C# ultimately derive from object.

Value Types vs Reference Types

In C#, types are divided into two main categories: Value Types and Reference Types.

Value Types

  • Definition: Store data directly.
  • Examples: All numeric types (int, double, etc.), bool, char, struct, enum.
  • Behavior: When you assign a value type variable to another, it copies the value. Changes to one do not affect the other.
1
2
3
int a = 5;
int b = a; // b is a copy of a
b = 10;    // a is still 5

Reference Types

  • Definition: Store a reference (address) to the actual data (object) on the heap.
  • Examples: string, object, arrays, classes, delegates, interfaces.
  • Behavior: When you assign a reference type variable to another, both refer to the same object. Changes through one reference affect the other.
1
2
3
4
5
6
7
string s1 = "hello";
string s2 = s1;
s2 = "world"; // s1 remains "hello" (strings are immutable)

int[] arr1 = {1, 2, 3};
int[] arr2 = arr1;
arr2[0] = 99; // arr1[0] is now 99 too (arrays are mutable)

Summary:

  • Value types: hold data directly, live on the stack (usually), fast copies, no shared state.
  • Reference types: hold references to data on the heap, shared state possible, can be null.

Next: Learn about arithmetic, relational, and logical operators in C#.