Allow Array.IndexOf to be Called as an Instance Method in C# #9401
Unanswered
ShieldOfSalvation
asked this question in
Language Ideas
Replies: 1 comment 2 replies
-
Those methods are defined as static on the |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
This proposal suggests allowing commonly used static methods on arrays—such as Array.IndexOf—to be callable as instance methods directly on array objects in C#, improving readability, intuitiveness, and consistency with other .NET collection APIs.
Motivation
C# has long required calling certain operations on arrays through the static System.Array class:
int index = Array.IndexOf(myArray, value);
However, in modern C#, this feels unintuitive compared to how similar operations are performed on List or other collection types:
int index = myList.IndexOf(value); // intuitive, object-oriented
Why not allow this?
int index = myArray.IndexOf(value); // not currently allowed
This limitation leads to:
Proposal
Allow Array to support instance method syntax for selected commonly used static methods, starting with:
Example usage:
Design Considerations
Alternatives
Drawbacks
Conclusion
This small but impactful change would improve C# ergonomics and bring consistency to working with arrays—especially for developers familiar with instance-based APIs. It aligns with the object-oriented principles of the language and reflects how developers naturally expect to interact with array-like structures.
Beta Was this translation helpful? Give feedback.
All reactions