![]() |
|
[C#] "+" and "-" operators override [Resolved] - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] "+" and "-" operators override [Resolved] (/showthread.php?tid=5062267) |
"+" and "-" operators override [Resolved] - Mefie - Nov 18, 2015 Hello!
If you read the title, you already read the question, but again: What do I need to write into my interface if I would like to require the classes implementing this interface to override "+" and "-" operators? Thank you! Update: OK. I have the following code Code static DateTime operator +(DateTime c1, DateTime c2);That I cannot put into an interface because Quote:Error 1 Interfaces cannot contain operators ... 16 25 Calendar How can I create an interface that requires classes to implement operator overload when an interface cannot contain operators? * - +Haze - Nov 18, 2015 I don't think you can override the functions of those operators at such a high level. In a lower level perspective then yes it's possible. But the question is, why would you want to do that? * - Mouse❤️ - Nov 18, 2015 https://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx * - +Haze - Nov 18, 2015 I guess I'm wrong then. Interesting post Mouse. * - Odus - Nov 18, 2015 (Nov 18, 2015 09:07 AM)+Haze Wrote: But the question is, why would you want to do that? It can make life a little easier when you're working with objects, but I'd still go for a method rather than an overload. Code public class ProgramWanted to try out the immediate return syntax from Mouse's post, hadn't seen that until now. * - Mefie - Nov 18, 2015 (Nov 18, 2015 09:07 AM)+Haze Wrote: I don't think you can override the functions of those operators at such a high level. In a lower level perspective then yes it's possible. But the question is, why would you want to do that? Because I was asked by Uni to do it so 🙂 Soon, I will get to know, but at the moment I do not have to know ( I know it is related to the objects we will use, though ) what we will we use it for to require it with an interface. (Nov 18, 2015 09:11 AM)Μouse Wrote: https://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx Thanks! I have just forgotten about this article. Actually, I remember, I found it not so long ago, but did not fully read because I had to do something and then did not return. Now, I read it and for second time I found what I wanted to know. Thank you! Update: (Nov 18, 2015 09:35 AM)Odus Wrote: It can make life a little easier when you're working with objects, but I'd still go for a method rather than an overload. I was just about writing an own test code from it when I saw your post. Thank you, now I am sure how to do it 👍 Update2: I have a new problem now... please check the thread update! * - +Haze - Nov 18, 2015 (Nov 18, 2015 09:35 AM)Odus Wrote: It can make life a little easier when you're working with objects, but I'd still go for a method rather than an overload. Shidd, had no idea this was even possible. Seems pretty cool ngl. C# is evolved a lot. * - Only3 - Nov 18, 2015 the problem with operators override is that its must be and used in the class * - Mouse❤️ - Nov 18, 2015 (Nov 18, 2015 02:00 PM)Only3 Wrote: the problem with operators override is that its must be and used in the classWell that's one of if not the most common usage for operator overloading, so it works, at least for this. * - Mefie - Nov 18, 2015 (Nov 18, 2015 02:00 PM)Only3 Wrote: the problem with operators override is that its must be and used in the class In the end, the solution was, I created a parent class of all object classes that could occur in use and implemented the operator overload in the parent class so that all of its children ( and all objects that may occur ) will have it. My code works now so it surely IS A solution, but if you have any more suggestion, I still am glad to listen 🙂
|