diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index 213055a1d48e7..1dbe944505b88 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -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" } ] } diff --git a/docs/csharp/language-reference/compiler-messages/expression-form-restrictions.md b/docs/csharp/language-reference/compiler-messages/expression-form-restrictions.md new file mode 100644 index 0000000000000..160eb65ae9214 --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/expression-form-restrictions.md @@ -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: + + + +- [**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()) + { + // 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. diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 81dedbab5cf4c..134f4cf632856 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -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: > @@ -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 @@ -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 @@ -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 diff --git a/docs/csharp/misc/cs0175.md b/docs/csharp/misc/cs0175.md deleted file mode 100644 index 929f08a65c6b1..0000000000000 --- a/docs/csharp/misc/cs0175.md +++ /dev/null @@ -1,46 +0,0 @@ ---- -description: "Compiler Error CS0175" -title: "Compiler Error CS0175" -ms.date: 07/20/2015 -f1_keywords: - - "CS0175" -helpviewer_keywords: - - "CS0175" -ms.assetid: cedd769d-8258-4235-a321-362981b9f84b ---- -# Compiler Error CS0175 - -Use of keyword 'base' is not valid in this context - - The [base](../language-reference/keywords/base.md) keyword must be used to specify a particular member of the base class. For more information, see [Constructors](../programming-guide/classes-and-structs/constructors.md). - - The following sample generates CS0175: - -```csharp -// CS0175.cs -using System; -class BaseClass -{ - public int TestInt = 0; -} - -class MyClass : BaseClass -{ - public static void Main() - { - MyClass aClass = new MyClass(); - aClass.BaseTest(); - } - - public void BaseTest() - { - Console.WriteLine(base); // CS0175 - // Try the following line instead: - // Console.WriteLine(base.TestInt); - base = 9; // CS0175 - - // Try the following line instead: - // base.TestInt = 9; - } -} -``` diff --git a/docs/csharp/misc/cs0186.md b/docs/csharp/misc/cs0186.md deleted file mode 100644 index de4e61db1d84a..0000000000000 --- a/docs/csharp/misc/cs0186.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -description: "Compiler Error CS0186" -title: "Compiler Error CS0186" -ms.date: 07/20/2015 -f1_keywords: - - "CS0186" -helpviewer_keywords: - - "CS0186" -ms.assetid: b8afca3e-0fb9-44c5-b4bb-abe3ef134e85 ---- -# Compiler Error CS0186 - -Use of null is not valid in this context - - The following sample generates CS0186: - -```csharp -// CS0186.cs -using System; -using System.Collections; - -class MyClass -{ - static void Main() - { - // Each of the following lines generates CS0186: - foreach (int i in null) {} // CS0186 - foreach (int i in (IEnumerable) null) { }; // CS0186 - } -} -``` diff --git a/docs/csharp/misc/cs1547.md b/docs/csharp/misc/cs1547.md deleted file mode 100644 index 298177d530c65..0000000000000 --- a/docs/csharp/misc/cs1547.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -description: "Compiler Error CS1547" -title: "Compiler Error CS1547" -ms.date: 07/20/2015 -f1_keywords: - - "CS1547" -helpviewer_keywords: - - "CS1547" -ms.assetid: 40029557-076a-47d8-aabc-d86c56a846d7 ---- -# Compiler Error CS1547 - -Keyword 'void' cannot be used in this context - - The compiler detected an invalid use of the [void](../language-reference/builtin-types/void.md) keyword. - - The following sample generates CS1547: - -```csharp -// CS1547.cs -public class MyClass -{ - void BadMethod() - { - void i; // CS1547, cannot have variables of type void - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md index 10affda335669..0d31e10203c57 100644 --- a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md +++ b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md @@ -157,9 +157,7 @@ f1_keywords: - "CS8111" - "CS8113" # C# 7.0 diagnostics - - "CS8115" - "CS8180" - - "CS8185" - "CS8188" - "CS8189" - "CS8190" @@ -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"