Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

General Lambda syntax question

So I seem to be confident that the following 2 statements are the same

List<object> values = new List<object>();
values.ForEach(value => System.Diagnostics.Debug.WriteLine(value.ToString()));

AND

List<object> values = new List<object>();
values.ForEach((object value) => { 
    System.Diagnostics.Debug.WriteLine(value.ToString());
});

AND I know I can insert multiple lines of code in the second example like

List<object> values = new List<object>();
values.ForEach((object value) => { 
    System.Diagnostics.Debug.WriteLine(value.ToString());
    System.Diagnostics.Debug.WriteLine("Some other action");
});

BUT can you do the same thing in the first example? I can't seem to work out a way.

like image 217
Maxim Gershkovich Avatar asked Dec 09 '22 08:12

Maxim Gershkovich


1 Answers

Yes you can :)

        List<object> values = new List<object>();
        values.ForEach(value =>
                           {
                               System.Diagnostics.Debug.WriteLine(value.ToString());
                               System.Diagnostics.Debug.WriteLine("Some other action");
                           }

            );
like image 120
Bashir Magomedov Avatar answered Dec 11 '22 23:12

Bashir Magomedov