Skip to content

Enumerable.MaxBy should describe behavior of throw Sequence contains no elements InvalidOperationException for empty primitive type enumerables #7461

@gewarren

Description

@gewarren

Issue moved from MicrosoftDocs/feedback#3721


From @Smoovsky on Thursday, December 2, 2021 5:39:28 AM

Describe the bug
I noticed new Sytem.Linq.Enumrable.MaxBy method provided in .NET 6.0:
doc
The documentation states two types of exception ArgumentException and ArgumentNullException to be expected.
But InvalidOperationException may also be thrown for empty primitive type enumerables, see the example below.

To Reproduce
Steps to reproduce the behavior:
Snippet:

void Main()
{
	var test1 = new Test[] { }.MaxBy(x => x.MyProperty); // no problem
	var test2 = new Test[] { new Test { MyProperty = 1 }, new Test { MyProperty = 1 } }.MaxBy(x => x.MyProperty); // no problem
	var test3 = new int[] { }.MaxBy(x => x); // exception here
}

public class Test
{ 
	public int MyProperty { get; set; }
}

Run result:
image

Metadata

Metadata

Assignees

Labels

🗺️ reQUESTTriggers an issue to be imported into Quest.Pri3Indicates issues/PRs that are low priorityarea-System.Linq

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions