![]() |
|
[C#] Comprehensive Lambda Tutorial - 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#] Comprehensive Lambda Tutorial (/showthread.php?tid=5001156) |
Comprehensive Lambda Tutorial - SIM-1725686 - Sep 26, 2015 Hi, HF. I wrote a tutorial on lambda functions today, and I thought it'd be a great idea to share it with you all. http://blog.thosakwe.com/2015/csharp-lambdas/ Feel free to leave me a comment, or share it with your friends. Thanks for reading! * - StayTech666 - Sep 27, 2015 Thanks for the tutorial! I've been meaning to learn lambdas for some time now and this definitely helps. Short, sweet and simple. Could you help me understand this line (where firstHalf and secondHalf are int arrays): Code var result = secondHalf.Zip(firstHalf, (f, s) => new[] { f, s }).SelectMany(f => f);* - StayTech666 - Sep 28, 2015 (Sep 28, 2015 01:15 AM)xadamxk Wrote: From what I understand, it's similar to the function operator? One of his examples is pretty easy to follow: Code public void Main()So, in that code he is declaring a function f that takes a variable x. The declaration of Func<int, int> is defining the input and output types (int for an input, int for an output) and the lambda expression is basically defining the actual return value, so it is simply x + 3. (Using the x we declared earlier.) Now, you can easily call this function by simply writing: Code public int AddThree(int valueToAdd)Here is another quick example: Code static void Main(string[] args)So all this does is take a string and put quotations around it. (Hello world! becomes "Hello world!") Lambdas are awesome! 👍 * - SIM-1725686 - Sep 28, 2015 (Sep 27, 2015 08:13 PM)StayTech666 Wrote: Thanks for the tutorial! I've been meaning to learn lambdas for some time now and this definitely helps. Short, sweet and simple. Thanks! I put effort into it to make it easy to read. As for the code, it does two things:
(Sep 28, 2015 12:46 PM)xadamxk Wrote: I suppose I understand it. I just don't know why this would be used before the conventional methods. It's convenient to use this with LINQ. Also, an advantage of lambdas is that you can see what you're doing, rather than having to scroll to other code that may be anywhere else. Even 5000 lines deep in another subfolder. It makes it easy to understand the code, basically. * - typeofBlue - Oct 6, 2015 Dont mean to nitpick or anything, but I think it would have been useful to show the other way of writing a anonymous method using the old delegate() {} way, since this might be more intiuitive for people. It helped me understand it better when I first learned it at least. Anyways good post, here's free bump ;) |