microsoft / microsoft/CsWinRT

Bug: Empty Array is projected as null instead of an array of length 0

Open
#2,088 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
665
Forks
134
Avg merge
1d 3h
Merged PRs (30d)
32

Description

Description

Consider the idl:

String[] GetItems();

If the implementation of this function returns an empty array, the CsWinRT projection will return null. But it is expected to return a non-null empty array.

The downside of empty arrays being projected as null is that it forces the consumer to write unnatural code:

string[] items = thing.GetItems();
if (items != null)
{
    foreach (var item in items)
    {
        Console.WriteLine(item);
    }
}

instead of the more idiomatic:

foreach (var item in thing.GetItems())
{
    Console.WriteLine(item);
}

@oldnewthing as FYI.

Steps To Reproduce

See description.

Expected Behavior

See description.

Version Info

2.2.0

Additional Context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the String[] GetItems() case described in the issue with an implementation that returns an empty array, then trace the CsWinRT projection path that produces the C# result. Done means an empty projected result is non-null, has length 0, and supports direct enumeration without a null check.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.