L
L
Ledington2021-12-26 11:22:51
C++ / C#
Ledington, 2021-12-26 11:22:51

How to return a collection for an IEnumerable extension method?

I have an extension method:

public static ExtensionMethod(this List<int> list, int percentline)
        {
            try
            {
                if (list.Count <= 100 && list.Count >= 1)
                {
                    IEnumerable<int> count = list.OrderByDescending(sort => sort).Take((int)(list.Count() * percentline / 100.0f));
                    Console.WriteLine("Перечень элементов: \n" + string.Join(", ", count));
                }
                else
                {
                    throw new ArgumentException();
                }
            }
            catch (ArgumentException)
            {
                Console.Write("ОШИБКА!!!\n");
            }
            return ?????
        }

Do I need to return the collection it was called on? How can I do it? Something I'm a little confused.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ayazer, 2021-12-26
@Ledington

public static IEnumerable<int> ExtensionMethod(this IEnumerable<int> list, int percentline)
        {
            try
            {
                if (list.Count() <= 100 && list.Count() >= 1)
                {
                    IEnumerable<int> count = list.OrderByDescending(sort => sort).Take((int)(list.Count() * percentline / 100.0f));
                    Console.WriteLine("Перечень элементов: \n" + string.Join(", ", count));
                }
                else
                {
                    throw new ArgumentException();
                }
            }
            catch (ArgumentException)
            {
                Console.Write("ОШИБКА!!!\n");
            }
            return list;
        }

well and accordingly further
var list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            list
                .ExtensionMethod(10)
                .ExtensionMethod(25);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question