Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Array.Empty<> over Enumerable.Empty<> #15186

Closed
wants to merge 1 commit into from
Closed

Use Array.Empty<> over Enumerable.Empty<> #15186

wants to merge 1 commit into from

Conversation

MikeAlhayek
Copy link
Member

No description provided.

@kevinchalet
Copy link
Member

Don't expect any noticeable perf' improvement (because well, Enumerable.Empty<TResult>() calls Array.Empty<T> internally 🤣), but it's a good opportunity to simplify the syntax by using [] instead of Enumerable.Empty<TResult>()/Array.Empty`.

@MikeAlhayek
Copy link
Member Author

@kevinchalet yes there is no advantage. just once less call :)

Also, I think [] is different than Array.Empty<>. Array.Empty<> returns the same instance every time whereas [] creates a new instances every time.

@kevinchalet
Copy link
Member

Also, I think [] is different than Array.Empty<>. Array.Empty<> returns the same instance every time whereas [] creates a new instances every time.

Nope: https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQMwAJboMLoN7LpGYZQAs6AsgBQCU+hxTUcADANoC66AhgE58eAT3QBedFwDcjIgF9ksoA= 😄

@MikeAlhayek
Copy link
Member Author

@kevinchalet that's awesome! and change it to a mutable object like a list autmatically create new List! thanks for the tip!

@kevinchalet
Copy link
Member

@kevinchalet that's awesome! and change it to a mutable object like a list autmatically create new List! thanks for the tip!

Yeah! And it uses CollectionsMarshal.AsSpan() to populate the list so it's very efficient 😄

Note: you can even use the range operator to build collections! E.g: https://github.com/openiddict/openiddict-core/blob/fb2565e298223940de7cf5e1c2fb3dd8de5bc516/src/OpenIddict.Client/OpenIddictClientHandlers.cs#L25-L162

@MikeAlhayek
Copy link
Member Author

Making this change part of #15187. closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants