Skip to content

Instantly share code, notes, and snippets.

@giggio
Created April 22, 2012 15:49
Show Gist options
  • Save giggio/2464791 to your computer and use it in GitHub Desktop.
Save giggio/2464791 to your computer and use it in GitHub Desktop.
Iterating over a dynamic list
using System.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace DynamicForeach
{
[TestClass]
public class ForeachTest
{
[TestMethod]
public void ForeachDynamicTest()
{
var a = new {A = 1};
var b = new {A = 1};
var c = new {A = 1, B = 2};
var array = new ArrayList {a, b, c};
var total = 0;
foreach (dynamic item in array)
{
total += item.A;
}
Assert.AreEqual(3, total);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment