![]() |
|
[C#] [Lambda] With or without - 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#] [Lambda] With or without (/showthread.php?tid=5763782) |
[Lambda] With or without - SirNoslyw - Dec 6, 2017 Hey 🤩 Umm, I was coding and then lambda expressions comes up in my mind. Which syntax is more clearer for you (in this situation)? (with lambda expression): private static MyClass MyMethod (object obj) => obj; __________ (without lambda expression): private static MyClass MyMethod(object obj) { return obj; } Normally I just use lambda expressions for delegates. RE: [Lambda] With or without - BahNahNah - Dec 6, 2017 First one. Though there are only a few situations where this would even come up (like overriding/implementing interface) because a property is usually more suited (assuming there are no parameters). And then, i would still use lambda Code public static MyClass MyProperty => [value];RE: [Lambda] With or without - 0xDEAD10CC - Dec 6, 2017 It depends on the size, captures you might want, and re-use of the code that your lambda encapsulates... The context here is irrelevant because it looks dumbed down for the purpose of your explanation, so nobody here is going to be able to provide you with a good answer. RE: [Lambda] With or without - BahNahNah - Dec 6, 2017 (Dec 6, 2017 03:02 PM)0xDEAD10CC Wrote: It depends on the size, captures you might want, and re-use of the code that your lambda encapsulates... The context here is irrelevant because it looks dumbed down for the purpose of your explanation, so nobody here is going to be able to provide you with a good answer. I assume the context is a function with one instruction/line? RE: [Lambda] With or without - Ninja - Dec 7, 2017 (Dec 7, 2017 05:51 AM)Orka Wrote: First is of course 'cleaner' but if you ever have to revise it to >1 line you must revert to the original syntax. I tend to use this for short overrides (operators, ToString, etc.), delegates and LINQ. Why is everyone saying that? I find the second one cleaner. Then again, I hardly ever use the first. RE: [Lambda] With or without - 0xDEAD10CC - Dec 7, 2017 If you find yourself shortening code down to LINQ just so that you can use the first, then you're probably just not thinking about readability. Depending too, properties might be a better choice. The example above doesn't really show much. RE: [Lambda] With or without - mrq5bok - Dec 10, 2017 I use the second one since it is cleaner and more understandable to use. RE: [Lambda] With or without - Crake - Dec 10, 2017 The former definitely is cleaner. EDIT: Removed me being an idiot. RE: [Lambda] With or without - byte[] - Dec 10, 2017 (Dec 7, 2017 11:45 AM)0xDEAD10CC Wrote: If you find yourself shortening code down to LINQ just so that you can use the first, then you're probably just not thinking about readability. Depending too, properties might be a better choice. The example above doesn't really show much. I agree, it's a broad example. |