C#
null values
int data type
nullable types
programming tips

How to set null value to int in c?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C#, a plain int cannot hold null because int is a non-nullable value type. If you need an integer that may be missing, the correct type is int?, which is shorthand for Nullable<int>.

Use int? for Nullable Integers

The most common syntax is:

csharp
int? age = null;
Console.WriteLine(age.HasValue); // false

This creates a value that can hold either an integer or null. The longer equivalent form is:

csharp
Nullable<int> age = null;

Both declarations mean the same thing. In everyday C# code, int? is preferred because it is shorter and easier to read.

Read and Default a Nullable int

Once you have a nullable integer, you usually need one of three things:

  • check whether it has a value
  • read the value safely
  • provide a fallback
csharp
1int? count = null;
2
3if (count.HasValue)
4{
5    Console.WriteLine(count.Value);
6}
7else
8{
9    Console.WriteLine("count is missing");
10}
11
12int safeCount = count ?? 0;
13Console.WriteLine(safeCount);

The null-coalescing operator ?? is often the cleanest way to supply a default.

Use Nullable int in Real Data Models

Nullable integers are common in data-transfer objects, database models, and forms where a value may be optional:

csharp
1public class Person
2{
3    public int? Age { get; set; }
4}
5
6var p = new Person();
7p.Age = null;

This expresses an important difference:

  • '0 means the value is known and equal to zero'
  • 'null means the value is missing, unknown, or not applicable'

That distinction matters a lot in business logic.

Convert Carefully

If you need to convert a nullable integer to a non-nullable one, decide how missing values should behave:

csharp
1int? maybeId = null;
2
3int id1 = maybeId ?? -1;                 // explicit fallback
4int id2 = maybeId.GetValueOrDefault();   // fallback to 0

Avoid reading .Value unless you know the variable is not null. Otherwise, you will get an exception.

Pattern matching can help make the code clearer:

csharp
1if (count is int actual)
2{
3    Console.WriteLine(actual);
4}

This reads more naturally than separate HasValue and Value access in some codebases.

Do Not Confuse Nullable Value Types with Nullable Reference Types

C# also has nullable reference type annotations such as string?, but that is a different feature. int? uses Nullable<T> because int is a value type. The surface syntax looks similar, but the runtime meaning is different.

That distinction becomes important in mixed models where a property such as string? Name and int? Age both appear optional but come from different parts of the type system.

Common Pitfalls

The biggest mistake is trying to assign null directly to a plain int:

csharp
int age = null; // invalid

Another common issue is using .Value without first checking HasValue. That throws InvalidOperationException when the value is null.

People also use 0 as a stand-in for "missing" when the domain actually needs to distinguish zero from unknown. In those cases, int? is the correct model.

Finally, when binding database values, make sure the ORM or mapping layer also treats the column as nullable. The CLR type alone does not fix a schema mismatch.

Summary

  • A plain int cannot be null in C#.
  • Use int? or Nullable<int> when the integer value may be missing.
  • Check HasValue, use .Value carefully, and prefer ?? for defaults.
  • Distinguish null from meaningful numeric values such as 0.
  • Model optional numeric data explicitly instead of inventing magic sentinel values.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.