Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing anonymous typed group to a function

I needed to pass a IGrouping on an anonymously typed index to a function.

 List<DataClass> sampleList = new List<DataClass>();
 var groups = sampleList.GroupBy(item => new { item.A, item.B, item.C }); 

I needed to process each of the groups with a function. So I wrote this which works.

static void ProcessGroup<T>(IGrouping<T, DataClass> group)
        { 
        //consume group

        }

Now I need to know why this works and Is it the right way to do pass around this kind of data. I just wrote it on a hunch that this might work.

like image 237
Midhat Avatar asked Mar 24 '26 19:03

Midhat


1 Answers

I need to know why this works

It works because that is a legal C# program as described by the C# specification. Specifically, section 7.5.2 of the C# 4 spec.

is it the right way to pass around this kind of data?

Looks fine to me.

like image 190
Eric Lippert Avatar answered Mar 27 '26 07:03

Eric Lippert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!