There is an extension method:

static public List<TResult> ToList<TResult>(this UIElementCollection c, TResult result) where TResult : class { var l = new List<TResult>(); foreach (var item in c) { l.Add((TResult)item); } return l; } 

But the problem appears when I call:

 var l = stack.Children.ToList(CheckBox); 

stack is StackPanel . where all its elements are CheckBox .

Writes that CheckBox type, which is not valid in this context.

How to implement correctly?

    1 answer 1

    Why do you need the second parameter?

     static public List<TResult> ToList<TResult>(this UIElementCollection c) where TResult : UIElement { var l = new List<TResult>(); foreach (var item in c) { l.Add((TResult)item); } return l; } 

    Using

     var l = stack.Children.ToList<CheckBox>(); 

    By and large this is the same as

     var l2 = stack.Children.Cast<CheckBox>().ToList(); 
    • really) y -
    • one
      @ Sanitary but you understand that the code is essentially the equivalent of var l2 = stack.Children.Cast<CheckBox>().ToList(); ? - tym32167
    • Well posuti yes so - Sasuke
    • just wanted to do so) - Sasuke