How to Store Any Value in an object Variable in C#
Most C# variables hold one type, but some values can be either: a size that's a name such as "medium" or a number of pixels such as 400. A variable of type object takes any value, and keeps track of what it really is.
Syntax
object <name> = <any value>;
if (<name> is <Type> <typedName>)
{
// use <typedName> as a <Type>
}
<Type> <typedName> = (<Type>)<name>;
| Part | What it does |
|---|---|
object <name> | A variable that can hold a value of any type |
<name>.GetType() | The type of the value it holds right now |
is <Type> <typedName> | Checks the type, and gives you the value as that type when it matches |
(<Type>)<name> | A cast: the value as <Type>, or an exception when it's another type |
Microsoft's built-in reference types reference: "The object type is an alias for System.Object in .NET." And: "Assign values of any type (except ref struct, see ref struct) to variables of type object."
The Example
A size variable holds the text "medium", then the number 400. The program prints each value with its type, then adds 100 to the number once it has it back as an int.
Prerequisites
- The .NET 10 SDK or newer, which runs a single
.csfile withdotnet run. This guide uses .NET SDK 10.0.404.
Step 1: Write the Code
Create a file named main.cs in an empty folder:

object size = "medium";
Console.WriteLine($"{size} is a {size.GetType().Name}");
size = 400;
Console.WriteLine($"{size} is a {size.GetType().Name}");
if (size is int pixels)
{
Console.WriteLine($"As a number: {pixels + 100}");
}
Step 2: Run It
dotnet run main.cs
medium is a String
400 is a Int32
As a number: 500

- The same variable held a
String, then anInt32, the .NET name ofint. size is int pixelsmatched, sopixelswas the number 400, andpixels + 100was 500.
The command is the same in zsh, bash and PowerShell.
Getting the Value Back
An object can't go straight into an int variable, even when it holds one:
object size = 400;
int pixels = size;
Console.WriteLine(pixels);
/Users/you/demo/main.cs(2,14): error CS0266: Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)
The build failed. Fix the build errors and run again.
The compiler only knows the variable's type, object. A cast says which type you expect:
object size = 400;
int pixels = (int)size;
Console.WriteLine(pixels + 100);
500
But a cast is a promise. When the variable holds something else, it fails while the program runs:
object size = "medium";
int pixels = (int)size;
Console.WriteLine(pixels);
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32'.
at Program.<Main>$(String[] args) in /Users/you/demo/main.cs:line 2
That's why is, as in Step 1, is the safer way to read an object you aren't sure about.
Boxing
Putting a number such as 400 into an object is called boxing, and taking it out with a cast is unboxing. Microsoft's Boxing and Unboxing guide: "Boxing is implicit; unboxing is explicit." That's the difference you saw: size = 400; needed nothing, and reading it back needed a cast.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Assigning an object to an int | error CS0266: Cannot implicitly convert type 'object' to 'int' | Use is int x, or a cast |
| Casting to the wrong type | InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32'. | Check with is first |
Using object where one type would do | Every reader has to check the type | Keep object for values that really can be several types |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
CS0266 ... from 'object' to ... | An object assigned to a typed variable | Cast it, or use is |
InvalidCastException | The value isn't the type you cast to | Print GetType().Name to see what it is |
FAQ
Why does GetType say Int32 and not int?
int is C#'s name for the .NET type System.Int32, and GetType().Name gives the .NET name. In the same way, string is System.String.
Is object the same as var?
No. var lets the compiler pick one type from the first value, and the variable keeps it: var size = "medium"; followed by size = 400; stopped with error CS0029: Cannot implicitly convert type 'int' to 'string'. An object variable can hold a value of a different type later, as size did in Step 1.
Conclusion
An object variable holds a value of any type, and GetType() tells you which. To use the value as its own type again, check it with is <Type> <name>, or cast it with (<Type>) when you're sure, because a wrong cast throws InvalidCastException.
Additional Resources
- Built-in reference types: object, on Microsoft Learn.
- Boxing and Unboxing, on Microsoft Learn.
- Type-testing operators and cast expressions, on Microsoft Learn.
