0

I am a beginner in c# and really interested in learning the language by going deeper. Recently used Lambda function to make a piece of my code but no idea how it worked.

I was mapping values from a list by writing hard code. See below:

var validRatings = new List<int> { 1, 2, 3, 4, 5 };

Used Lambda and it worked:

var validRatings = settings.grossAlphas.Select(ga => ga.fundRating).ToList();

I understand Lambda functions are anonymous but how are they different than regular functions and how do they fit in OOP context within C#?

Mihir Patel
  • 2,202
  • 3
  • 23
  • 36
  • What do you mean how do they fit into object oriented context? – nbokmans Feb 24 '17 at 16:40
  • 1
    Lambdas are a form of expression. You need to research LINQ. That said, this is not a site for tutorials; you won't get much help unless you show an actual problem with the associated code. – CDove Feb 24 '17 at 16:41
  • Well, anonymous functions are just useful when you don't need and want to get rid of everything else and leave what is most important to you. – Jerry Switalski Feb 24 '17 at 16:41
  • Lambdas are from functional programming. C# is a mix of OOP and functional programming concepts. You can readily see how they're different--they aren't defined on an object. –  Feb 24 '17 at 16:42
  • If Lambda works differently in Functional vs OOP. – Mihir Patel Feb 24 '17 at 16:42

4 Answers4

6

Lambda functions are anonymous but how are they different than regular functions?

"How are X and Y different?" is in general not a good question for StackOverflow. Here, here's an apple tree and an oak tree; how are they different? Hard to say.

Briefly though:

  • Lambdas can be converted to delegates or expression trees. Regular functions can only be converted to delegates.

  • Lambdas can be expression bodied or statement bodied. Before C# 6, regular functions could only be statement bodied.

  • Lambdas can have their formal parameter types inferred from context; regular functions cannot.

how do they fit in OOP context within C#?

They don't. Lambdas are an idea from functional programming, not from object oriented programming. The fundamental idea of OOP is that functionality is logically connected to data; lambdas are the exact opposite of that. Lambdas are about the decoupling of functions from data objects.

If that doesn't answer your question, then ask a more clear question. What do you mean by "the oop context"?

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
1

A normal method can be called by name anytime in the code, but there are cases when we need some block of code that would be executed just once or the caller is only one, so if you see in the above code.

The main purpose of lambda expressions is to give calling code flexibility to pass it's own implementation, for example in the above case, you can have specified your own implementation and project my original collection to a new collection which contains one property named fundRating, chances are you only need this block of code for this use case, so lambda expression creates anonymous method which can only be called by the Select() method implementation.

Hope it helps!

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
1

You could create a function and name it, like so:

private string getFundRating(GrossAlpha ga)
{
    return ga.fundRating;
}

And then you could pass that function in your Select. like so:

var validRatings = settings.grossAlphas.Select(getFundRating).ToList();

This would be an example of not using a lambda. In that case you are using a named function. You could use the same function somewhere else in your code or in another linq statement.

In the example you provided, you create a function on the fly, don't even name it and pass it in the Select.

Note: I assumed you had an object named GrossAlpha, and that settings.grossAlphas returns a list of those.

Regarding the other part of your question. How do they fit in the context of OOP, I can't explain it much better than Eric Lippert did.

Olivier De Meulder
  • 2,493
  • 3
  • 25
  • 30
0

A simple way to think of an anonymous method, albeit not a pure definition, is a method that you want to use once and move on. They're called anonymous because the aren't named methods. Lambdas, in C#, are an extremely powerful way of linking commonly used methods based on your individual objects.

What your code is doing, in one line would take many lines to write out unique methods based on every one of your classes.

To break down what your code is doing dot-by-dot:

var validRatings = settings.grossAlphas.Select(ga => ga.fundRating).ToList();

grossAlphas.Select(...): is walking through your enumeration (List, Array, etc...) object by object and aggregating them according to the parameters you supply inside the .Select(...).

ga => ga.fundRating: you are saying that you want to aggrigate ONLY the fundRating attribute from each object.

.ToList(): is making your enumeration into a List<int>().

This saves you from writing a List<int> GetFundRatings() for a single time you need to use a list of FundRatings.

Also see:

What is a lambda (function)?

C# Lambda expressions: Why should I use them?

Community
  • 1
  • 1
Kaden.Goodell
  • 162
  • 1
  • 8