How to count all defined childforms with C#

I tried to use Count() or Length to count how many Can_ListCandidate childfrom are opening. Then, if there is only 1 form run, it is still keeping. While if the number is more than 2, they all will be closed. However, there is no option for the f.GetType() == typeof(Can_ListCandidate) to return any number.

foreach(Form f in this.MdiChildren) {
  if (f.GetType() == typeof(Can_ListCandidate)) {
    f.Dispose();
  }
}
Jon Skeet
people
quotationmark

It's somewhat unclear to me what you're trying to do, but if you want to count with a predicate, you could use:

int count = MdiChildren.OfType<Can_ListCandidate>().Count();

which includes subclasses or

int count = MdiChildren.Count(x => x.GetType() == typeof(Can_ListCandidate));

which doesn't.

Neither of these will dispose the forms, of course...

people

See more on this question at Stackoverflow