Skip to content
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions .openpublishing.redirection.csharp.json
Original file line number Diff line number Diff line change
Expand Up @@ -5840,6 +5840,21 @@
{
"source_path_from_root": "/docs/csharp/programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md",
"redirect_url": "/dotnet/csharp/fundamentals/expressions/equality#implement-equality-yourself-when-a-type-cant-be-a-record"
},
{
"source_path": "docs/csharp/misc/cs0175.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/expression-form-restrictions#invalid-expression-contexts",
"redirect_document_id": "false"
},
{
"source_path": "docs/csharp/misc/cs0186.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/expression-form-restrictions#invalid-expression-contexts",
"redirect_document_id": "false"
},
{
"source_path": "docs/csharp/misc/cs1547.md",
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/expression-form-restrictions#invalid-expression-contexts",
"redirect_document_id": "false"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
title: "Resolve errors from invalid expression contexts"
description: "This article helps you diagnose and correct C# compiler errors and warnings from expressions that appear in contexts where they are not permitted"
f1_keywords:
- "CS0175"
- "CS0186"
- "CS1547"
- "CS8115"
- "CS8185"
- "CS8209"
- "CS8310"
- "CS8312"
helpviewer_keywords:
- "CS0175"
- "CS0186"
- "CS1547"
- "CS8115"
- "CS8185"
- "CS8209"
- "CS8310"
- "CS8312"
ms.date: 08/20/2026
ai-usage: ai-assisted
---

# Resolve errors from invalid expression contexts

This article covers the following compiler errors and warnings:

<!-- The text in this list generates issues for Acrolinx, because it doesn't use contractions.
That's by design. The text closely matches the text of the compiler error or warning for SEO purposes.
-->

- [**CS0175**](#invalid-expression-contexts): *Use of keyword 'base' is not valid in this context*
- [**CS0186**](#invalid-expression-contexts): *Use of null is not valid in this context*
- [**CS1547**](#invalid-expression-contexts): *Keyword 'void' cannot be used in this context*
- [**CS8115**](#invalid-expression-contexts): *A throw expression is not allowed in this context.*
- [**CS8185**](#invalid-expression-contexts): *A declaration is not allowed in this context.*
- [**CS8209**](#invalid-expression-contexts): *A value of type 'void' may not be assigned.*
- [**CS8310**](#invalid-expression-contexts): *Operator 'operator' cannot be applied to operand 'operand'*
- [**CS8312**](#invalid-expression-contexts): *Use of default literal is not valid in this context*

## Invalid expression contexts

The following diagnostics identify expressions that appear in contexts where they're not permitted. These errors typically arise when you use keywords, literals, or expressions in positions where the compiler doesn't permit them. The remediation strategy depends on the specific keyword or expression type.

- **CS0175**: *Use of keyword 'base' is not valid in this context*. Use the `base` keyword to access a specific member of the base class. Don't use it as a standalone expression. When you need to reference the base class, access a member explicitly: `base.MemberName` instead of just `base`. For example, avoid `Console.WriteLine(base);` and instead use `Console.WriteLine(base.Member);`. Similarly, don't attempt to assign to `base` directly; instead, assign to a specific base member: `base.Member = value;`. Ensure you always specify which base class member you need to access. This error also occurs when using `base` outside of an instance method in a derived class.

- **CS0186**: *Use of null is not valid in this context*. You can't use the `null` literal in contexts where the compiler expects a concrete collection or enumerable. This error commonly occurs in `foreach` loops where `null` is provided as the collection to iterate over. In a `foreach` loop, the collection must implement `IEnumerable` (or similar interface) and must be non-null. If your data source might be null, check for null before the loop:

```csharp
IEnumerable collection = /* your source */;
if (collection != null)
{
foreach (var item in collection)
{
// Process item
}
}
```

Alternatively, use a null-coalescing operator or default empty collection:

```csharp
foreach (var item in collection ?? Enumerable.Empty<T>())
{
// Process item
}
```

- **CS1547**: *Keyword 'void' cannot be used in this context*. The `void` keyword indicates the absence of a return value and can't be used as a variable type or field type. It's valid only as a method return type (`void Method() { }`), a delegate return type (`public delegate void Action();`), or a pointer-to-void in unsafe code (`void* ptr;`). If you encounter this error, you're likely attempting to declare a variable of type `void`, which is invalid. Instead, choose an appropriate type for the variable. If you need a method that performs an action without returning a value, call it as a standalone statement (`MyMethod();`); don't attempt to assign its result.

- **CS8115**: *A throw expression is not allowed in this context.*. The compiler permits throw expressions only in specific contexts where an expression can appear and the exception is immediately propagated. Move the `throw` expression to a valid context, such as a conditional arm of a ternary or switch expression (where the throw is one of the arms), an assignment to evaluate the throw in place of the assigned value, or an argument to a method call where throwing is appropriate. A throw expression can also appear in a statement in a lambda or local function body (not within a method that must return a value). If the throw expression appears in a position where the expression value must be used (such as within arithmetic or operator expressions), extract it into a separate statement or conditional check.

- **CS8185**: *A declaration is not allowed in this context.*. The compiler permits declaration expressions (`out var`, pattern-matching declarations) only in specific positions, including `out` parameter declarations in method calls and in certain statement contexts. Remove the declaration expression from contexts where it's not permitted, such as lambda expression bodies (unless the lambda is a statement body), query expressions where the grammar forbids declarations, inside attribute arguments, or in contexts that require a read-only expression. If you need to use an out variable, call the method in a separate statement, then reference the resulting variable. Alternatively, use a local variable declaration before the expression.

- **CS8209**: *A value of type 'void' may not be assigned.*. The `void` type isn't a value type; it represents the absence of a return value. Remove the assignment of void-returning expressions. If you need to invoke a method that returns `void`, call it as a standalone statement. If you need a result, use a method that returns a value instead. Ensure that expressions assigned to variables always have a meaningful return type.

- **CS8310**: *Operator 'operator' cannot be applied to operand 'operand'* and **CS8312**: *Use of default literal is not valid in this context*. The `default` literal and typeless expressions (such as `null` or `new` without a target type) require a target type for the compiler to infer the expression type. When an operator is applied to these expressions without enough context, the compiler can't determine the operand type. Provide the target type by adding an explicit type cast (`(int)default` or `(MyType)new`), assigning to a typed variable (`int x = default;`), using a method parameter or return type to establish context, or using the verbose form `default(Type)` instead of the `default` literal for clarity. If the operator itself requires a specific type, ensure the operand can be implicitly converted to that type.
12 changes: 6 additions & 6 deletions docs/csharp/language-reference/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,12 @@ items:
CS1688, CS1706, CS1731, CS1732, CS1764, CS1911, CS1989, CS3006, CS8030, CS8175,
CS8820, CS8821, CS8916, CS8917, CS8934, CS8971, CS8972, CS8974, CS8975, CS9098,
CS9099, CS9100, CS9236
- name: Expression-form restrictions
href: ./compiler-messages/expression-form-restrictions.md
displayName: >
keyword contexts, base keyword, null, void, throw expressions,
declaration expressions, invalid contexts,
CS0175, CS0186, CS1547, CS8115, CS8185, CS8209, CS8310, CS8312
- name: Local functions
href: ./compiler-messages/local-function-errors.md
displayName: >
Expand Down Expand Up @@ -991,8 +997,6 @@ items:
href: ./compiler-messages/cs0173.md
- name: CS0174
href: ../misc/cs0174.md
- name: CS0175
href: ../misc/cs0175.md
- name: CS0176
href: ../misc/cs0176.md
- name: CS0177
Expand All @@ -1001,8 +1005,6 @@ items:
href: ../misc/cs0179.md
- name: CS0180
href: ../misc/cs0180.md
- name: CS0186
href: ../misc/cs0186.md
- name: CS0191
href: ../misc/cs0191.md
- name: CS0198
Expand Down Expand Up @@ -1363,8 +1365,6 @@ items:
href: ../misc/cs1542.md
- name: CS1545
href: ../misc/cs1545.md
- name: CS1547
href: ../misc/cs1547.md
- name: CS1548
href: ./compiler-messages/cs1548.md
- name: CS1551
Expand Down
46 changes: 0 additions & 46 deletions docs/csharp/misc/cs0175.md

This file was deleted.

31 changes: 0 additions & 31 deletions docs/csharp/misc/cs0186.md

This file was deleted.

32 changes: 0 additions & 32 deletions docs/csharp/misc/cs1547.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ f1_keywords:
- "CS8111"
- "CS8113"
# C# 7.0 diagnostics
- "CS8115"
- "CS8180"
- "CS8185"
- "CS8188"
- "CS8189"
- "CS8190"
Expand All @@ -168,15 +166,12 @@ f1_keywords:
- "CS8202"
- "CS8205"
- "CS8206"
- "CS8209"
# C# 7.1 diagnostics
- "CS8300"
- "CS8301"
- "CS8305"
- "CS8308"
- "CS8309"
- "CS8310"
- "CS8312"
# C# 7.2 diagnostics
- "CS8323"
- "CS8328"
Expand Down
Loading