← Back to Articles         
checked and unchecked operation & default, typeof, and nameof

checked and unchecked operation & default, typeof, and nameof

Published on 12 Oct 2026     5 min read C#
C# Fundamentals
 

C# Operators and Expressions

1. 🔴 What are checked and unchecked operations in C#?

In C#, checked and unchecked are keywords used to control how arithmetic overflow is handled for integral types.

Overflow occurs when the result of a calculation exceeds the range supported by its data type.

For example, the maximum value of int is 2,147,483,647.

1. checked Operation

The checked keyword detects arithmetic overflow and throws an OverflowException when an overflow occurs at runtime.

Example:

int number = int.MaxValue;

checked
{
    int result = number + 1;
    Console.WriteLine(result);
}

Output:

OverflowException

Explanation:

  • int.MaxValue is 2,147,483,647.

  • Adding 1 exceeds the maximum value.

  • checked detects the overflow.

  • An OverflowException is thrown.

Execution Flow:

int.MaxValue = 2147483647
          |
          v
       Add 1
          |
          v
   Overflow Occurs
          |
          v
   checked Operation
          |
          v
   OverflowException

2. unchecked Operation

The unchecked keyword allows arithmetic overflow without throwing an exception for the affected integral operations.

The result wraps around according to the data type's range.

Example:

int number = int.MaxValue;

unchecked
{
    int result = number + 1;
    Console.WriteLine(result);
}

Output:

-2147483648

Explanation:

  • int.MaxValue is 2,147,483,647.

  • Adding 1 exceeds the maximum value.

  • unchecked does not throw an overflow exception.

  • The result wraps around to int.MinValue.

Execution Flow:

int.MaxValue = 2147483647
          |
          v
       Add 1
          |
          v
   Overflow Occurs
          |
          v
  unchecked Operation
          |
          v
  Result = -2147483648

Difference Between checked and unchecked

checked

unchecked

Detects arithmetic overflow

Allows arithmetic overflow

Throws OverflowException

Does not throw for integral overflow

Useful when overflow must be detected

Useful when wraparound is intentional

Example: Financial calculations

Example: Certain bitwise algorithms

Key Points

  1. checked detects overflow and throws an exception.

  2. unchecked allows integral overflow and wraps the result.

  3. Both can be used as blocks or expressions.

  4. They primarily apply to integral arithmetic and conversions.

  5. Runtime integral arithmetic is generally unchecked by default unless configured otherwise.

  6. Constant expressions are checked by default at compile time.

  7. For financial calculations, decimal is generally preferred over double; checked does not make floating-point arithmetic exact.

Interview Answer: checked detects arithmetic overflow and throws an OverflowException, whereas unchecked allows integral overflow without throwing an exception and produces a wrapped result.

 

2. 🟢 What is the difference between default, typeof, and nameof in C#?

In C#, default, typeof, and nameof are operators or expressions used to retrieve default values, type information, and names of program elements.

1. default Operator

The default operator returns the default value of a specified data type.

Example:

int number = default(int);
bool isActive = default(bool);
string? name = default(string);

Console.WriteLine(number);   // 0
Console.WriteLine(isActive); // False
Console.WriteLine(name);     // Empty line (null)

Common Default Values:

Data Type

Default Value

int

0

double

0.0

bool

false

char

'\0'

string

null

DateTime

DateTime.MinValue

int?

null

Modern C# Syntax:

int number = default;
bool isActive = default;
string? name = default;

Here, the compiler identifies the target type automatically.

2. typeof Operator

The typeof operator returns a System.Type object containing information about a specified type.

Example:

Type type = typeof(string);

Console.WriteLine(type.Name);
Console.WriteLine(type.FullName);

Output:

String
System.String

Explanation:

  • typeof(string) retrieves information about the string type.

  • Name returns the simple type name.

  • FullName returns the namespace-qualified type name.

Real-Time Example:

Type type = typeof(Employee);

Console.WriteLine(type.Name);

Output:

Employee

typeof is commonly used in reflection, dependency registration, and attribute processing.

3. nameof Operator

The nameof operator returns the name of a variable, class, method, property, or other named program element as a string.

Example:

string employeeName = "John";

Console.WriteLine(nameof(employeeName));

Output:

employeeName

Explanation:

  • nameof(employeeName) returns the variable name.

  • It does not return the variable's value.

  • The result is determined at compile time.

Another Example:

class Employee
{
    public string Name { get; set; } = "";
}

Console.WriteLine(nameof(Employee));
Console.WriteLine(nameof(Employee.Name));

Output:

Employee
Name

Real-Time Example: Parameter Validation

nameof is useful when throwing exceptions.

public void SaveEmployee(string name)
{
    if (string.IsNullOrEmpty(name))
    {
        throw new ArgumentException(
            "Employee name is required",
            nameof(name));
    }
}

Explanation:

  • nameof(name) returns "name".

  • If the parameter is renamed during refactoring, the compiler uses the updated name.

  • This avoids hardcoding parameter names as strings.

Difference Between default, typeof, and nameof

Feature

default

typeof

nameof

Purpose

Gets default value

Gets type information

Gets element name

Returns

Value of specified type

System.Type

string

Example

default(int)

typeof(int)

nameof(number)

Result

0

Type information for Int32

"number"

Common Usage

Initialization

Reflection

Validation and exceptions

Key Points

  1. default returns the default value of a data type.

  2. typeof returns type metadata as a System.Type object.

  3. nameof returns the name of a program element as a string.

  4. default is commonly used in initialization and generic programming.

  5. typeof is commonly used in reflection and dependency injection.

  6. nameof helps avoid hardcoded strings and improves refactoring safety.

Interview Answer: default returns the default value of a type, typeof returns a System.Type object representing a type, and nameof returns the name of a program element as a string.

 

Quick Interview Revision

Concept

Key Point

checked

Detects overflow and throws an exception

unchecked

Allows integral overflow without an exception

int.MaxValue

2147483647

int.MinValue

-2147483648

default(int)

Returns 0

default(bool)

Returns false

typeof(string)

Returns System.Type information

nameof(employeeName)

Returns "employeeName"

Important Interview Tip: Remember these two differences: checked vs unchecked controls integral overflow behavior, while default, typeof, and nameof return a default value, type information, and a program element's name, respectively.

← Back to Articles