Simplifying Code with C# Operators
HTML-код
- Опубликовано: 16 окт 2024
- In this session, I'm going to show you one of my favorite features in C#. This session is about defining custom operators in C# for a custom type and leveraging it to simplify your code and make it much more readable.
Read more about C# operators in here:
learn.microsof...
True, a common conception of the operator for those types is vital, otherwise they could do more harm than good.
vector1 + vector2 is a great use.
student1 + student2 is something that would force me to read the definition of the operator anyway or make possibly wrong assumptions. With a normal method, apart from its name, you could also get a short description when you hover over it.
With operators you'll also get description on hover if you write it ()
Thank you for your video. One thing I would like to see at the end, is examples - when do you use that in a real enterprise applications. In what cases it is most useful or make code much more readable. There are a lot information about operators in different places, but if you add your experience to that information it makes it unique and much more valuable. Thank you once more.
The argument you made for not using the plus sign is a good one. Those reading code using the standard forms " new List {name1, name 2} or list.Add(...)" immediately know what's happening. No explanation needed.
Then there's the issue of needing more than two items in a list: name1 + name 2 + name3. Taking it even further, you end up in the territory of: list + name4.
This seems like its more trouble than its worth. Also, I know you saw comments like this coming :-)
Now I wonder if I can pass params - might solve that problem.
But I agree - these videos are more introductory and exploratory, no adaptation yet.
Great video. Thank you.
Great explanation but hassan if you explain both (pros & cons) sides of any feature then it will be more perfect and it will help us in applying the feature more accurately.
Thanks
Interesting use of operators. I do this a lot for the implicit and explicit operators so that I can cast in defined and understood ways, especially so that I don't need to override ToString, which in my opinion is the most overloaded extension (not an operator I know) there is. But having the ability to convert numbers to words or even just the string representation built into the class is very powerful. Just think of all the ways you can make a money object behave with just these two operators. Then include add and substract making sure you can't add to monetary values together if they are not the same currency? This is a great topic and we don't see enough use of the operator functions imo. Thanks for sharing.
Hey Hassan, I like your teaching style. Can please make a series on .NET and the C# language.
Of course. Check out this series
ruclips.net/p/PLan3SCnsISTTt1vJEaaHkC8a6j70_DmfY
VERY NICE!
Nice video.. But what about performance? It's benchmarked? I'm wondering what if a tons of elements not just merge 2 items
Thanks, Can you please explain a bit about on explicit/implicit operators..
This is the oposite of clean. Operator overloading was introduced to facilitate scientific code, the usage shown here is a gross antipattern.
I don't know what '>>' means. I'm a web developer and looking to be better at using c# for common math and statistical operations. But aside from the normal operators, i rarely see tutorials on how to use C# for maths that is slightly beyond the basics, but not too advanced. I don't even know how to choose types correctly, like float and double, or how to write write 2 *2 * 2, with an exponent(if that is what it's called)
Whilst shorthanding initialization is always appreciated in any language I worry if you were to take this code to the logical conclusion which would be expressed as "var allStudents = michael + naveen;" which does not read well at all and each default operator implies some kind of action which is NOT what you are trying to achieve. Also the operator code itself on the student class is 'stacking' new instances of data structures on the return which is a huge no no if you care about the memory and efficiency of your application. Operator overloads seemed like a good idea and I'm sure they have one but I am not convinced by this demo that this is the way in which they should be used, this does not create CLEAN code when you add a layer of indirection to the reader.
operator overloading is generally discouraged due to the ambiguity you introduce in your api. they're fine for by yourself, but *generally awful if you have you maintain, revisit, or collab on this code in some way.
it's a great feature, but the example is really bad and the disadvantages are being majorly undersold here.
Wow
Love these shorter vids Hass. You have a great mind worth paying attention to, but i'm Mr. Short-Attention-Span, so although i've been subscribed for... a couple years(?), i've generally declined watching your vids because they're too long and also because your mind tends to meander around in the fields around the subject matter while i'm shouting at my screen "Show me some code baby!!".
hey, again, love your mind. And i dunno if i should even talk, here's my attempt at paying it forward for all the great developer content i enjoy on youtube:
ruclips.net/p/PL5iQTfLIfQQhYZ6QHouKew5Nr09IUQXxG
my thinking was i'd just record myself writing code, kinda like J. Kenji Lopez Alt does his cooking vids (ruclips.net/video/tEHH88sWi7Q/видео.html), but yeah, i'm far from that good. So my product is... yeah, it is what it is.
IMHO, Web Dev Simplified (ruclips.net/video/w0VEOghdMpQ/видео.html) is the gold standard for presentation of content: He's tightly focused around the task at hand, offers excellent support with links for any topic that may require a deeper understanding of the viewer, the pace is just right, overall, just great stuff. He reminds me of the late great K. Scott Allen.
good stuff Hass, good stuff. Thank you for your vids!!
Thank you for your feedback, truly appreciate it.
Yeah no. Where to begin... let's say you want to create three element list instead of the convenient two. Now you find yourself making another "plus" overload to handle a list and a Student. Then you'd also like the operator to be commutative. These always sound pretty appealing but after some thought the conclusion is almost always the same: not the proper usage of operator overloading.
This doesn't make the code cleaner, it makes it so you have to find the definition of all these operators and add cognitive load to whoever has to maintain your code.