Programming

Cannot assign void to an implicitly-typed variable in C#

Fix 'Cannot assign void to an implicitly-typed variable' in C#: why Split(...).Reverse() may bind to Array.Reverse, missing System.Linq, and practical fixes.

1 answer 1 view

Why does Visual Studio 2026 report the build error “Cannot assign void to an implicitly-typed variable” for the following line, even though the same code builds in Visual Studio 2022?

cs
var provValue = registryValue.Split(',').Reverse();

Target framework: .NET Framework 4.7.2 (Visual Studio 18.1.1). The code is in the RegistryValueKind.QWord branch:

cs
case RegistryValueKind.QWord:
 var provValue = registryValue.Split(',').Reverse();
 registryValue = string.Join("", provValue);
 long qValue = (long)Convert.ToUInt64(registryValue, 16);
 childKey.SetValue(valueData[0], qValue, (RegistryValueKind)registryType);
 break;

What causes the compiler to say “Cannot assign void to an implicitly-typed variable”? Is this due to a missing using (System.Linq), an ambiguity with Array.Reverse (which returns void), or a bug in Visual Studio 2026? How can I fix this?

You’re seeing “Cannot assign void to an implicitly-typed variable” because the compiler resolved your call to a void-returning Reverse (so the expression has no value for var to infer) instead of LINQ’s Enumerable.Reverse<T> which returns an IEnumerable<T>. The usual causes are a missing using System.Linq; or an accidental ambiguity with a void Reverse (for example Array.Reverse), though differences in project references or implicit/global usings between VS2022 and VS2026 can also change which Reverse is in scope. Fixes: add using System.Linq;, call System.Linq.Enumerable.Reverse(...), explicitly type the variable, or materialize the sequence with .ToArray()/.ToList().


Contents


Why the compiler says “Cannot assign void to an implicitly-typed variable”

var makes the compiler infer a type from the right-hand expression. That only works if the expression produces a value. Methods that return void do not produce a value, so code like this fails:

cs
var x = Array.Reverse(someArray); // error: Cannot assign void to an implicitly-typed variable

You get the same error text when the expression you wrote ends up being a call that returns void. In your case the right-hand side (registryValue.Split(',').Reverse()) is being resolved to a void-returning overload or method, so var can’t infer anything and the compiler complains. See the general diagnosis in this write-up and common StackOverflow threads about the same error: https://www.wayline.io/blog/cannot-assign-void-to-an-implicitly-typed-variable and https://stackoverflow.com/questions/22814302/cannot-assign-void-to-an-implicitly-typed-local-variable-with-var-and-foreach.


Array.Reverse vs Enumerable.Reverse (System.Linq)

Two different “Reverse” APIs are commonly confused:

  • System.Array.Reverse(Array) — a static method that mutates the array and returns void. It’s declared on System.Array and is used like:
cs
var arr = registryValue.Split(',');
Array.Reverse(arr); // returns void, in-place reverse

Docs: https://learn.microsoft.com/en-us/dotnet/api/system.array.reverse?view=net-7.0

  • System.Linq.Enumerable.Reverse(this IEnumerable) — an extension method (in the System.Linq namespace) that returns IEnumerable<TSource> and yields the sequence in reverse order (does not mutate the input). To use .Reverse() instance-style you need using System.Linq; in scope:
cs
using System.Linq;
var rev = registryValue.Split(',').Reverse(); // returns IEnumerable<string>

If the compiler can’t see the LINQ extension (no using System.Linq; or it’s shadowed), you may accidentally be invoking something else or the binder may report that the expression produces void. Several StackOverflow questions illustrate the confusion and show the difference in behavior: https://stackoverflow.com/questions/50236935/trouble-using-array-reverse and https://softwareengineering.stackexchange.com/questions/287369/beginners-c-question-about-array-reverse.


How to fix this in your code — practical options

Pick the approach that fits your intent:

  1. If you want a reversed sequence (deferred/enumerable) — bring LINQ into scope:
cs
// top of file
using System.Linq;

var provValue = registryValue.Split(',').Reverse();
registryValue = string.Join("", provValue);

That makes .Reverse() bind to Enumerable.Reverse<T> and the code compiles.

  1. Fully qualify the LINQ call (no using needed):
cs
var provValue = System.Linq.Enumerable.Reverse(registryValue.Split(','));
  1. Explicitly type the local (helps clarify intent and avoid var confusion):
cs
IEnumerable<string> provValue = registryValue.Split(',').Reverse();
registryValue = string.Join("", provValue);
  1. If you want an array mutated in-place, use Array.Reverse as a separate statement:
cs
var parts = registryValue.Split(',');
Array.Reverse(parts); // void — do it on its own line
registryValue = string.Join("", parts);
  1. Materialize the reversed sequence if you need an array/list:
cs
using System.Linq;
var provValue = registryValue.Split(',').Reverse().ToArray(); // ToArray requires LINQ
registryValue = string.Join("", provValue);

Any of the above will resolve the “assign void” error by ensuring the RHS evaluates to a value type instead of void.


Why it could build in VS2022 but fail in VS2026 — diagnosis steps

Why the difference? A few likely causes — check these in order:

  • Missing using System.Linq; in the file or a different set of global/implicit usings between the two IDEs/projects. Newer templates and SDKs sometimes add implicit/global usings; older projects targeting .NET Framework typically do not.
  • A conflicting symbol named Reverse (a static helper or extension method returning void) introduced in your solution or a referenced assembly. Search the solution for static .* Reverse( or Reverse(this to spot custom extensions.
  • Different project references or compiler/language versions between the VS2022 build and the VS2026 build that change overload resolution or symbol visibility.
  • Less likely: a Roslyn/compiler regression in the VS2026 toolset. If you can reproduce the failure with a tiny standalone repro (a single .cs file and the same target framework) and using System.Linq is present, then gather that repro and consider filing a bug with the Roslyn team.

Quick diagnostics you can run now:

  • Hover / F12 / Peek Definition on .Reverse() to see which symbol the IDE binds to.
  • Add using System.Linq; and rebuild — if it fixes the error, scope was the problem.
  • Replace the RHS with System.Linq.Enumerable.Reverse(...) — if that fixes it, same conclusion.
  • Search for other Reverse methods in your code/references.

If none of these show the cause and you have a minimal repro, try compiling on the command line or a different machine/VS version; if the behavior is inconsistent, collect the repro and file an issue.


Sources


Conclusion

The compiler error means the RHS is voidvar can’t infer a type from nothing. In practice the fix is to ensure you’re calling LINQ’s Enumerable.Reverse (which returns a value) rather than Array.Reverse (which returns void) or any other void-returning method with the same name. Start by adding using System.Linq; or fully qualifying System.Linq.Enumerable.Reverse(...); if that doesn’t help, search for conflicting Reverse definitions or differences in project/global usings between VS2022 and VS2026 and produce a minimal repro before assuming a compiler bug.

Authors
Verified by moderation
Moderation
Cannot assign void to an implicitly-typed variable in C#