There are numerous ways to generate it.
1. Using binary arithmetic.
using System.Collections.Generic;
namespace PowerSets
{
class Solution1
{
public static IList<IList<T>> PowerSet<T>(IList<T> list)
{
int n = 1 << list.Count;
IList<IList<T>> powerset = new List<IList<T>>();
for (int i = 0; i < n; ++i)
{
IList<T> set = new List<T>();
for (int bits = i, j = 0; bits != 0; bits >>= 1, ++j)
{
if ((bits & 1) != 0)
set.Add(list[j]);
}
powerset.Add(set);
}
return powerset;
}
}
}
2. Using iteration.
using System.Collections.Generic;
using System.Linq;
namespace PowerSets
{
public class Solution2
{
public static IList<IList<T>> PowerSet<T>(IList<T> list)
{
IList<IList<T>> powerset = new List<IList<T>>() { new List<T>() };
foreach (T item in list)
{
foreach (IList<T> set in powerset.ToArray())
{
powerset.Add(new List<T>(set) { item });
}
}
return powerset;
}
}
}
3. Using recursion.
using System.Collections.Generic;
using System.Linq;
namespace PowerSets
{
public class Solution3
{
static public IList<IList<T>> PowerSet<T>(IList<T> list)
{
if (list.Count == 0)
{
return new List<IList<T>>(new[] { new List<T>() });
}
else
{
IList<IList<T>> ps = PowerSet(list.Skip(1).ToArray());
foreach (List<T> ss in ps.ToArray())
{
ps.Add(new List<T>(ss) { list.First() });
}
return ps;
}
}
}
}
Rosetta Code has lots of example you can refer for.