Trick question: The difference between an int and a struct?

8/21/2023

During C# interviews (as the interviewer), I commonly ask about value types and reference types. Put concisely (more for conciseness than accuracy): Value types are things like structs, enums, bools and ints. They are stored on the stack, and are passed by value. Reference types refer to things inheriting from System.Object. They are stored on the heap. These very important foundational concepts the programmers should understand.

But there's another question I occasionally ask, more out of curiosity if they know the answer. It's not important because programmers can write a lot of C# not knowing this interesting piece of fact. The question is...

What's the difference between an int and a struct?

Short answer: ints are structs

Long answer

According to this documentation, there are only two categories of value types:

Structs can be broken into these categories:

  • Numeric types: ints, floats, decimals
  • bool
  • User defined structs

These all are derived from System.ValueType.

So there's you answer

It turns out that ints (and floats, decimals and bools) all derive from structs. In fact in the documentation here, it actually refers to Int32 as a struct.

So now you're ready for that interview question...


Please register or login to add a comment.

Comments (displaying 1 - 1):
No comments yet! Be the first...


  • C#/.NET/Core
  • T-SQL
  • HTML/Javascript/jQuery
  • ASP.NET/MVC
  • .NET Core
  • ADO.NET/EF
  • WPF
  • Xamarin/MAUI
  • Windows 10
  • SQL Server 20xx
  • Android
  • XBox One
  • Skiing
  • Rock Climbing
  • White water kayaking
  • Road Biking