How can I override operators to be used on builtin types like String, arrays etc? For example: I wish to override the meaining of the + operator for arrays.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can’t overload operators for existing types, as that could potentially break any other code that uses the types.
You can make your own class that encapsulates an array, expose the methods and properties that you need from the array, and overload any operators that makes sense.
Example:
Usage:
(Note that as the class implements
IEnumerable<T>, you can use extension methods likeSelecton it.)