Click here to Skip to main content
12,553,488 members (54,894 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked

Passing Anonymous Types with Dynamic Lists

, 1 Oct 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
With the dynamic keyword C# is one step ahead of me allowing a future of amazing code determined by an unlimited number of factors at runtime.

Recently I was rewriting a function with a large method signature which took several arrays as parameters.  As you might guess, the index across these arrays was assumed to be the same which is error-prone.  I thought we should really encapsulate all this information in to a class or struct and pass in a single list instead.  Then I stopped myself as there was no use for this class beyond this one-time call to another function.

Then I thought, I know, I could use an anonymous type instead.

var datum = new { a = myData.a, b = myData.b, /* c, d, ..., */ z = myData.z };

This seems like a reasonable approach and exactly what throw-away anonymous types are for.  Then I tried to add this to a list.  Hmm, with a little creativity I was able to overcome this… except it only worked within the scope of the same function.  Well, I could have passed a list of objects, but what would I cast them to?

I really thought I’d have to cave in and just create the new class but then there was one option I hadn’t considered: dynamic.  Like JavaScript, this frees you from the restriction of static typing which was inhibiting a simple notion to pass a list of anonymous types to another function.  Now our list definition looks like this:

var data = new List<dynamic>();

Now this means I could really add in anything and its evaluation will be resolved at runtime.  So we’re finally free to use our anonymous class however we want.  We could even code access to properties which we aren’t supplying and code will compile (however you’ll get a nasty surprise at runtime).

protected void Bob()
    List<dynamic> data = new List<dynamic>();

    //Assume lots of processing to build up all these arguments
    data.Add(new { a = 1, b = "test" });


private void Fred(List<dynamic> data)
    //Fred processing logic
    foreach (var datum in data)
        Trace.WriteLine(String.Format("{0}: {1}", datum.a, datum.b));

The more evolved my client coding becomes the more restraining statically typed languages can feel.  With the dynamic keyword C# is one step ahead of me allowing a future of amazing code determined by an unlimited number of factors at runtime.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Architect Avaya Inc.
Ireland Ireland
Formerly a C++ client developer, nowadays I'm all about C# and ASP.NET. Over the years I have mastered some and played with many aspects of .NET.

Follow my blog as I catalogue the more arcane problems I encounter and their solutions at

You may also be interested in...


Comments and Discussions

QuestionHow to create dynamically ? Pin
superb14-Jun-14 13:13
membersuperb14-Jun-14 13:13 
QuestionA "cheap" solution that would be "expensive" later... Pin
Carlos19072-Oct-13 0:30
memberCarlos19072-Oct-13 0:30 
AnswerRe: A "cheap" solution that would be "expensive" later... Pin
newkie2-Oct-13 1:12
membernewkie2-Oct-13 1:12 
GeneralRe: A "cheap" solution that would be "expensive" later... Pin
Carlos19072-Oct-13 7:49
memberCarlos19072-Oct-13 7:49 
GeneralPerfect Pin
afaz1-Oct-13 21:56
memberafaz1-Oct-13 21:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 1 Oct 2013
Article Copyright 2013 by Chris_Green
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid