Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 62 additions & 20 deletions docs/csharp/misc/cs0184.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: "Compiler Warning (level 1) CS0184"
description: "Learn about compiler warning CS0184: The given expression is never of the provided ('type') type, its common causes, and how to fix or suppress it."
title: "Compiler Warning (level 1) CS0184"
ms.date: 07/20/2015
ms.date: 11/01/2026
f1_keywords:
- "CS0184"
helpviewer_keywords:
Expand All @@ -10,22 +10,64 @@ ms.assetid: 55e73f76-f502-4d15-88fc-bd5757b512a4
---
# Compiler Warning (level 1) CS0184

The given expression is never of the provided ('type') type

The expression can never be **true** because the variable you are testing is neither declared as ***type*** nor derived from ***type***.

The following sample generates CS0184:

```csharp
// CS0184.cs
// compile with: /W:1
class MyClass
{
public static void Main()
{
int i = 0;
if (i is string) // CS0184
i++;
}
}
> The given expression is never of the provided ('type') type

Compiler warning CS0184 occurs when the compiler detects that an [`is`](../language-reference/operators/type-testing-and-cast.md#the-is-operator) expression or type pattern test is guaranteed to evaluate to `false` at compile time. This occurs when there is no implicit or explicit conversion from the compile-time type of the operand to the target type.

## Example

The following sample generates CS0184:

```csharp
// CS0184.cs
// compile with: /W:1
class MyClass
{
public static void Main()
{
int number = 0;
if (number is string) // CS0184: 'number' is of type 'int', which can never be 'string'
{
number++;
}
}
}
```

## How to fix this warning

To resolve CS0184:

1. **Remove redundant code**: If the check is unintended or impossible, remove the unreachable `is` expression and any dead code in the conditional branch.
2. **Correct the target type**: If you intended to test against a different type or check a polymorphic base class or interface, update the target type to match your design.
3. **Use an appropriate cast or type conversion**: If you need to convert between unrelated types (such as converting an `int` to a string representation), use explicit parsing or formatting methods like <xref:System.Convert> or `ToString()` rather than a type test.

## How to suppress this warning

If you cannot change the code—for example, when working with generic types, macro expansion, or generated code where an expression might never match the target type in a particular scenario—you can suppress the warning:

- **In source code**: Use the [`#pragma warning`](../language-reference/preprocessor-directives.md#pragma-warning) directive to disable and restore the warning around the specific line:

```csharp
#pragma warning disable CS0184
if (value is string)
{
// Specific handling
}
#pragma warning restore CS0184
```

- **In the project file**: Suppress the warning for the entire project by adding CS0184 to the `<NoWarn>` property in your `.csproj` file, or by passing the [`-nowarn:CS0184`](../language-reference/compiler-options/errors-warnings.md#nowarn) compiler option:

```xml
<PropertyGroup>
<NoWarn>$(NoWarn);CS0184</NoWarn>
</PropertyGroup>
```

## See also

- [Type-testing operators and cast expressions (`is` operator)](../language-reference/operators/type-testing-and-cast.md#the-is-operator)
- [Pattern matching overview](../fundamentals/patterns/pattern-matching.md)
- [#pragma warning](../language-reference/preprocessor-directives.md#pragma-warning)
- [-nowarn (C# Compiler Options)](../language-reference/compiler-options/errors-warnings.md#nowarn)