-1

** I have function that get an object as a parameter then trying to cast it as IEnumrable and check if it’s empty or at least have value Is there any better way to do that than the below**

Private bool empty(Object obj)
{
    Bool empty=true;
    If (obj is IEnumerable lst)
    {
        foreach (var item in IEnumerable lst)
        {
        empty=false;
        break;
        }
}
Andy
  • 12,859
  • 5
  • 41
  • 56
Oshehata
  • 1
  • 2
  • 3
    use `System.Linq` and use the extension `.Any()` – Andy Nov 15 '21 at 05:36
  • 3
    Does this answer your question? [How to check if IEnumerable is null or empty?](https://stackoverflow.com/questions/5047349/how-to-check-if-ienumerable-is-null-or-empty) – Harish Nov 15 '21 at 05:38
  • I think it might be better to explain why you are in this situation, and what problem you are trying to solve. – TheGeneral Nov 15 '21 at 05:46

1 Answers1

0

You could use Any().

Note. Normally you want to avoid IEnumerable and it's better to use IEnumerable<T>.

tymtam
  • 31,798
  • 8
  • 86
  • 126