Get users from groups in active directory, disposing of everything I am trying to get active directory groups with their users, all while disposing of all IDisposable assets when they reach the end of their usefulness. The groups and users are passed into a method that extracts certain properties and adds them to ExpandoObjects. This requires a decent number of nested using blocks.

Since the method is private, and most of the variables only exist in the using blocks, I gave the variables very short names. Naming them properly as groupPrincipal or principalSearchResult would involve breaking even more lines to fit the long names.

private IEnumerable&lt;ExpandoObject&gt; GetOuGroupsUsersData(
    PrincipalContext pc)
{
    var data = new List&lt;ExpandoObject&gt;();
    using (var gp = new GroupPrincipal(pc))
    {
        using (var ps = new PrincipalSearcher(gp))
        {
            using (var psr = ps.FindAll())
            {
                foreach (var g in psr.GetGroupPrincipals())
                {
                    using (var gpsr = g.GetMembers())
                    {
                        data.AddRange(
                            gpsr.GetUserPrincipals().Select(
                                u =&gt; GenerateData(g, u))
                                .Cast&lt;ExpandoObject&gt;());
                    }
                }
            }
        }
    }
    return data;
}


Is there anything I can do to reduce the number of using blocks? Am I even disposing of everything properly?
You can remove braces between using statements, leaving the last one. That would help you to remove a few of lines, but still not so deeply.
Wrapping the IDisposable interface is a good choice to do as an alternative to a dozen of using statements.
I'd suggest you using more appropriate names, maybe by lowercasing just a character of your class/method when you instantiate/call them.


Add this class to your project:

    public class IDisposableList : List&lt;IDisposable&gt;, IDisposable
    {
        public void Dispose()
        {
            if (this.Count &gt; 0)
            {
                List&lt;Exception&gt; exceptions = new List&lt;Exception&gt;();

                foreach (var disposableItem in this)
                {
                    try
                    {
                        disposableItem.Dispose();
                    }
                    catch (Exception ex)
                    {
                        exceptions.Add(ex);
                    }
                }

                base.Clear();

                if (exceptions.Count &gt; 0)
                    throw new AggregateException(exceptions);
            }
        }

        public T Add&lt;T&gt;(Func&lt;T&gt; item) where T : IDisposable
        {
            var disposable = item();
            base.Add(disposable);

            return disposable;
        }
    }


usage:

            var data = new List&lt;ExpandoObject&gt;();

            using (var disposables = new IDisposableList())
            {

                var gp = disposables.Add(() =&gt; new GroupPrincipal(pc));

                var ps = disposables.Add(() =&gt; new PrincipalSearcher(gp));

                var psr = disposables.Add(() =&gt; ps.FindAll());

                foreach (var g in psr.GetGroupPrincipals())
                {

                    var gpsr = disposables.Add(() =&gt; g.GetMembers());

                    data.AddRange(
                            gpsr.GetUserPrincipals().Select(
                                u =&gt; GenerateData(g, u))
                                .Cast&lt;ExpandoObject&gt;());

                }

            }


this is just a more elegant way to do what you asked.

Remember that using a foreach is not as performant as a for loop.