none
Generics Function generates error RRS feed

  • Question

  • I am pretty new to generics, and I want a simple function that takes In generic list and a generic item and returned whether the item is in the list (yes, I this is possible using the any() and other linq methods but I want to do more things in that function but just check if the item exists)

    I have tried to use:

    private static bool IsExist(List<T> list, T item) 
     {
    //code comes here...
     }

    But I get an error.

    why Isn't it working?

         
    Monday, October 14, 2019 5:34 PM

Answers

  • This is a template to work with

    public static class Extensions
    {
        private static bool IsExist<T>(List<T> list, T item)
        {
            return true;
        }
    }
    public static class Extensions
    {
        private static bool IsExist<T>(ICollection<T> list, T item)
        {
            return list.Contains(item);
        }
    }



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Monday, October 14, 2019 7:26 PM
    Moderator