Skip to content

Emit explicit element type for nullable value type arrays in CSharpHe… - #38712

Open
ilkertskn wants to merge 1 commit into
dotnet:mainfrom
ilkertskn:fix/csharp-helper-nullable-value-type-array
Open

Emit explicit element type for nullable value type arrays in CSharpHe…#38712
ilkertskn wants to merge 1 commit into
dotnet:mainfrom
ilkertskn:fix/csharp-helper-nullable-value-type-array

Conversation

@ilkertskn

@ilkertskn ilkertskn commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

…lper

  • A non-empty array literal is normally emitted as an implicitly-typed array (new[] { ... }), but nullable value type elements are rendered as their underlying type, so the array is inferred as the non-nullable type (e.g. int[] instead of int?[]), producing code that does not compile
  • Emit the element type explicitly for nullable value type arrays, matching the existing handling for byte and object arrays
  • Add tests for nullable value type arrays with and without null elements
  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

…lper

- A non-empty array literal is normally emitted as an implicitly-typed array (new[] { ... }),
  but nullable value type elements are rendered as their underlying type, so the array is
  inferred as the non-nullable type (e.g. int[] instead of int?[]), producing code that does
  not compile
- Emit the element type explicitly for nullable value type arrays, matching the existing
  handling for byte and object arrays
- Add tests for nullable value type arrays with and without null elements
@ilkertskn
ilkertskn requested a review from a team as a code owner July 29, 2026 07:04
@ilkertskn

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes C# literal generation for arrays of nullable value types in CSharpHelper by ensuring the emitted array literal preserves the intended nullable element type (e.g. int?[]) instead of being inferred as the underlying non-nullable type (e.g. int[]), which can cause generated code to fail compilation when assigned/used as T?[].

Changes:

  • Emit an explicit element type for non-empty arrays when the element type is a nullable value type (Nullable<T>), avoiding incorrect implicit array type inference.
  • Add coverage to CSharpHelperTest for nullable value type arrays both with and without null elements.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
test/EFCore.Design.Tests/Design/Internal/CSharpHelperTest.cs Adds tests asserting correct literal emission for int?[] arrays (with/without null).
src/EFCore.Design/Design/Internal/CSharpHelper.cs Updates array literal emission to explicitly include the nullable element type for Nullable<T> arrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants