Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 583737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:49:27+00:00 2026-05-13T14:49:27+00:00

In my quest to understand the very odd looking ‘ => ‘ operator, I

  • 0

In my quest to understand the very odd looking ‘ => ‘ operator, I have found a good place to start, and the author is very concise and clear:

parameters => expression

Does anyone have any tips on understanding the basics of lambdas so that it becomes easier to ‘decipher’ the more complex lambda statements?

For instance: if I am given something like (from an answer I received here):

filenames.SelectMany(f => 
        Assembly.LoadFrom(f).GetCustomAttributes(typeof(PluginClassAttribute), true)
        .Cast<PluginClassAttribute>()
        .Select(a => a.PluginType)
).ToList();

How can I go about breaking this down into more simple pieces?


UPDATE: wanted to show off my first lambda expression. Don’t laugh at me, but I did it without copying someone’s example…and it worked the first time:

public ModuleData[] GetStartModules( )
{ return modules.FindAll(start => start.IsBatch == true).ToArray(); }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:49:27+00:00Added an answer on May 13, 2026 at 2:49 pm

    Let’s dissect your code sample:

    filenames.SelectMany(f => 
            Assembly.LoadFrom(f).GetCustomAttributes(typeof(PluginClassAttribute), true)
            .Cast<PluginClassAttribute>()
            .Select(a => a.PluginType)
    ).ToList();
    

    So, we start off with a string[] called filenames. We invoke the SelectMany extension method on the array, and then we invoke ToList on the result:

    filenames.SelectMany(
       ...
    ).ToList();
    

    SelectMany takes a delegate as parameter, in this case the delegate must take one parameter of the type string as input, and return an IEnumerable<T> (Where the type of T is inferred). This is where lambdas enter the stage:

    filenames.SelectMany(f => 
            Assembly.LoadFrom(f).GetCustomAttributes(typeof(PluginClassAttribute), true)
    ).ToList()
    

    What will happen here is that for each element in the filenames array, the delegate will be invoked. f is the input parameter, and whatever comes to the right of => is the method body that the delegate refers to. In this case, Assembly.LoadFrom will be invoked for filename in the array, passing he filename into the LoadFrom method using the f argument. On the AssemblyInstance that is returned, GetCustomAttributes(typeof(PluginClassAttribute), true) will be invoked, which returns an array of Attribute instances. So the compiler can not infer that the type of T mentioned earlier is Assembly.

    On the IEnumerable<Attribute> that is returned, Cast<PluginClassAttribute>() will be invoked, returning an IEnumerable<PluginClassAttribute>.

    So now we have an IEnumerable<PluginClassAttribute>, and we invoke Select on it. The Select method is similar to SelectMany, but returns a single instance of type T (which is inferred by the compiler) instead of an IEnumerable<T>. The setup is identical; for each element in the IEnumerable<PluginClassAttribute> it will invoke the defined delegate, passing the current element value into it:

    .Select(a => a.PluginType)
    

    Again, a is the input parameter, a.PluginType is the method body. So, for each PluginClassAttribute instance in the list, it will return the value of the PluginType property (I will assume this property is of the type Type).

    Executive Summary
    If we glue those bits and pieces together:

    // process all strings in the filenames array
    filenames.SelectMany(f => 
            // get all Attributes of the type PluginClassAttribute from the assembly
            // with the given file name
            Assembly.LoadFrom(f).GetCustomAttributes(typeof(PluginClassAttribute), true)
            // cast the returned instances to PluginClassAttribute
            .Cast<PluginClassAttribute>()
            // return the PluginType property from each PluginClassAttribute instance
            .Select(a => a.PluginType)
    ).ToList();
    

    Lambdas vs. Delegates
    Let’s finish this off by comparing lambdas to delegates. Take the following list:

    List<string> strings = new List<string> { "one", "two", "three" };
    

    Say we want to filter out those that starts with the letter “t”:

    var result = strings.Where(s => s.StartsWith("t"));
    

    This is the most common approach; set it up using a lambda expression. But there are alternatives:

    Func<string,bool> func = delegate(string s) { return s.StartsWith("t");};
    result = strings.Where(func);
    

    This is essentially the same thing: first we create a delegate of the type Func<string, bool> (that means that it takes a string as input parameter, and returns a bool). Then we pass that delegate as parameter to the Where method. This is what the compiler did for us behind the scenes in the first sample (strings.Where(s => s.StartsWith("t"));).

    One third option is to simply pass a delegate to a non-anonymous method:

    private bool StringsStartingWithT(string s)
    {
        return s.StartsWith("t");
    }
    
    // somewhere else in the code:
    result = strings.Where(StringsStartingWithT);
    

    So, in the case that we are looking at here, the lambda expression is a rather compact way of defining a delegate, typically referring an anonymous method.

    And if you had the energy read all the way here, well, thanks for your time 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quest I am looking to fetch rows that have accented characters. The encoding for
In my quest for a version-wide database filter for an application, I have written
In my quest for my first iPhone app I have posted about the correct
In my quest to understand queries against Django models, I've been trying to get
In my quest to generate new code in a Scala compiler plugin, I have
I'm on a quest to reduce my very bloated home page application in development.
Minor quest - looking for a clean way to dynamically set the size of
In my quest for looking for a BigInt library, I came across this post:
On my quest to implement a very simple 'drag' mechanism to my application (which
I'm still on my eternal quest to build (and understand) modern programming convention of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.