Share via

C# lambda operator reliability

Asad Salam 1 Reputation point
2021-02-13T09:17:31.377+00:00

i want to know about the reliabilty of the code written with or without lambda expression?

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-02-13T10:09:04.82+00:00

    I use lambda expression code all of the time. I prefer it over regular code writing. Using lambda expressions really exposes object oriented programming and the power of using OOP to the developer, which is just as reliable as code that doesn't use lambda.

    https://www.codingame.com/playgrounds/213/using-c-linq---a-practical-overview/lambda-expressions#:~:text=A%20lambda%20expression%20is%20a,a%20delegate)%20as%20a%20parameter.

    Many code evaluation tools like Resharper encourage the use of lambda expressions and will change non lambda code to lambda code where applicable.

    There is nothing wrong in using lambda expressions.

    public async Task<List<DtoAuthor>> GetAll()
            {
                var dtos = new List<DtoAuthor>();
    
                var authors =  await pc.Author.ToListAsync();
    
                dtos.AddRange(authors.Select(**author => new DtoAuthor()**
                {
                    AuthorId = author.AuthorId,
                    FirstName = author.FirstName,
                    LastName = author.LastName
                }).ToList());
    
                return dtos;
            }
    

    Because I was able to use lambda on an AddRange().Select(), I didn't have to use a foreach loop to poputae a dtoAuthor from Author and load the dto into dtos list. The AddRange() did it all due to the lambda expression being used.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

  2. Alberto Poblacion 1,571 Reputation points
    2021-02-13T12:11:13.927+00:00

    If you are worried about the reliability, do not have any worries. I use lambdas extensively in code that gets executed millions of times and they do not create the slightest problem.
    In fact, lambdas are only syntactic sugar for the compiler. Once the code is compiled, they get translated into plain ordinary IL code which is the same that would have been generated if you had written the classes and method manually without expressing them as lambdas. So there is no reason to worry about their reliability.
    That said, be careful if you are capturing external variables into your lambdas by means of a closure. It's not that this is not reliable, just that it does not always behave as you would expect, unless you understand the way in which this is implemented internally.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.