How to Use is not with a Declaration Pattern in C#
if (input is not int number) return ...; turns away anything that isn't an int, and on every line after that if, number holds the value as an int. One check, no cast, and the rest of the method can rely on the number.
The patterns reference describes not as a pattern that matches an expression when the negated pattern doesn't match the expression. Put it in front of a declaration pattern, and the C# guide's description of that pattern still holds: The designation declares the variable that receives the matching value. The variable gets its value only when the value matches, which is exactly when the is not test is false.
Syntax
if (<value> is not <Type> <variable>)
{
return <result>;
}
// <variable> holds the value as a <Type> from here on.
| Part | What it is |
|---|---|
<value> | The value to test, such as an object or an int? |
is not <Type> <variable> | true when <value> isn't a <Type>. When it is one, the pattern stores it in <variable>, and the test is false |
return <result>; | Leaves the method, so the code after the if runs only for a <Type>. continue and throw work too |
<variable> | Usable after the if, already a <Type> |
The compiler keeps track of this. The C# guide: The compiler tracks whether a local variable receives a value before your code reads it. This tracking is called definite assignment.
The Example
A console app called IsNotPattern has a DoubleNumber method that takes any object. It returns a message for anything that isn't an int, then doubles the number with the variable the pattern declared.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n IsNotPattern
cd IsNotPattern
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/IsNotPattern/IsNotPattern.csproj:
Determining projects to restore...
Restored /Users/you/source/IsNotPattern/IsNotPattern.csproj (in 61 ms).
Restore succeeded.
dotnet new console -n IsNotPattern creates a folder named IsNotPattern with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or IsNotPattern.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the values and the loop:
object[] inputs = [5, "hello", 12];
foreach (object input in inputs)
{
Console.WriteLine(DoubleNumber(input));
}
object[] can hold values of any type: here two int values and a string.
Then the method:
static string DoubleNumber(object input)
{
if (input is not int number)
{
return $"{input} is not a whole number";
}
// The if returned for everything else, so number holds an int here.
return $"{number} doubled is {number * 2}";
}
input is not int numberistruewheninputisn't anint, and then the method returns a message.- When
inputis anint, the pattern stores it innumberand the test isfalse, so the method skips the block. number * 2uses the variable after theif. The only way to get there is past a match, so the compiler knowsnumberhas a value.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
5 doubled is 10
hello is not a whole number
12 doubled is 24

5 and 12 are int values: the method skipped the block and doubled them to 10 and 24. hello is a string, so the is not test was true, the method returned early, and the line that uses number never ran for it.
The Complete File

object[] inputs = [5, "hello", 12];
foreach (object input in inputs)
{
Console.WriteLine(DoubleNumber(input));
}
static string DoubleNumber(object input)
{
if (input is not int number)
{
return $"{input} is not a whole number";
}
// The if returned for everything else, so number holds an int here.
return $"{number} doubled is {number * 2}";
}
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using the variable inside the if block, as in return $"{number} is not a whole number"; | error CS0165: Use of unassigned local variable 'number' | Use input in the block: there the value didn't match, so number has nothing in it |
An if block that doesn't leave, such as one that only prints a message | error CS0165: Use of unassigned local variable 'number', on the line after the if | End the block with return, continue or throw, so the code after it runs only after a match |
The same name in two is not checks in one method, as in first is not int number and second is not int number | error CS0128: A local variable or function named 'number' is already defined in this scope | Give each its own name: with first is not int a and second is not int b, 2 and 3 added up to Sum: 5. The variable belongs to the whole method, not just the if. The C# specification: After the block the variable v is in scope, but not definitely assigned. |
FAQ
Doesn't Microsoft's error reference say you can't declare a variable under not?
The page on pattern-matching errors says You can't declare variables within not or or pattern combinators, with the error CS8780. For a not that is the whole pattern of an is expression, as in this guide, the declaration is allowed: the example built and ran, because the variable is assigned whenever the is expression is false. I reported the missing exception to the .NET docs as issue #57060.
Is it the same as !(input is int number)?
It behaves the same: with if (!(input is int number)), 5 was doubled to 10 and hello got the same message. The .NET style rule IDE0083 prefers the shorter form by default: Prefer to use the not pattern, when possible.
Does it work with a nullable value such as int?
Yes, and it unwraps it. With int? age, if (age is not int years) returned No age given for null, and for 30 the code after it printed Next year: 31, using years as a plain int.
Can the block use continue or throw instead of return?
Yes. In a loop, continue skipped hello while 5 and 12 were doubled. In a method, throw stopped the string "ten" with System.ArgumentException: ten is not a whole number, while 10 came back halved as 5.
Conclusion
Write if (value is not Type name) with a block that returns, continues or throws, and use name on the lines after it. Use the original value inside the block, and give each check in a method its own variable name.
Additional Resources
- Logical patterns — the
notpattern and how it combines with other patterns. - Declaration, constant, and var patterns — declaration patterns and definite assignment.
