Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

If IEnumerable<t> has a "Contains" method on:https://msdn.microsoft.com/en-us/library/9eekhta0(v=vs.110).aspx[^]

why is a code like this not working:

C#
using System;
using System.Collections.Generic;

namespace Mod1Assignement
{
    class Program
    {
        static void Main(String[] args)
        {
            List<int> aList = new List<int>();
            aList.Add(1);
            aList.Add(2);
            aList.Add(3);

            IEnumerable<int> test = aList;

            //Getting error on "Contains"
            bool x = test.Contains(2);
        }
    }
}


Please help me understand the reason. I am aware there are alternative solutions, but I would like to understand why isn't it working.

Thx
Posted
Updated 6-May-15 3:35am
v3

IEnumerble.Contains is working, but your code doesn't use the return boolean value; try:

bool isContained = test.Contains(3); // true

But, casting a List<int> to IEnumerable is a waste of time: all generic List<> implement IEnumerable:

bool isIEnumerable = aList is IEnumerable; // true
 
Share this answer
 
Comments
Suvendu Shekhar Giri 5-May-15 21:04pm    
I have a doubt, what if i do not keep returned value? Will it throw an error/exception? Why? Are you sure?
BillWoodruff 5-May-15 21:38pm    
Ignoring the return value does not cause an error in C#. But, why would you execute code that produced a return value and then ignore it ?

I have to wonder why you are asking this; what did you observe that made you ask this question; what evidence did you see that indicated your code was nor working ?
Suvendu Shekhar Giri 5-May-15 22:00pm    
I got it. You are absolutely correct. But OP's question suggests that he/she is experiencing an exception "IEnumerable<t> does not contain definition for 'Contains'" and that's why I was wondering how can your proposed solution will help resolving the issue. And yes, you are absolutely correct that there is no point of calling Contains without considering it's return value. Hope, you got my point.
BillWoodruff 5-May-15 23:23pm    
Oh, Sri Suvendu, I'm sorry, I thought your comment was from the OP, and my response was directed at him/her, not you ... not enough coffee in my brain yet :) And, yes, you would think there was a reason the OP asked the question !
Suvendu Shekhar Giri 6-May-15 0:17am    
Np :) my proposed solution alone cannot help the OP. Please see my updated solution.
Make sure that you have included System.Linq namespace in your file. Many times, people get exception like this for not including the Linq namespace as the Contains method belongs to that namespace.

Hope, it helps :)

Update
Also consider the solution proposed by @BillWoodruff as my solution alone can not do your job. It doesn't make sense to call 'Contains()' without dealing with the returned value.
 
Share this answer
 
v5
Comments
BillWoodruff 5-May-15 23:27pm    
+5 This is the most logical explanation for what the OP reports !
Suvendu Shekhar Giri 6-May-15 0:04am    
Thanks :)
Extreamely glad that you liked it. Will be more happy if it helps the OP.
Paulo Augusto Kunzel 6-May-15 9:01am    
Hello Suvendu,

Thanks for the reply, although I had considered Linq as a way to solve the issue, the thing that is bugging me is the reason why I get:
"Error 1 'System.Collections.Generic.IEnumerable<int>' does not contain a definition for 'Contains' and no extension method 'Contains' accepting a first argument of type 'System.Collections.Generic.IEnumerable<int>' could be found (are you missing a using directive or an assembly reference?)"
And on IEnumerable should implement Contains(). Or Am I missing something?

Regards
BillWoodruff 6-May-15 9:39am    
Suvendu's analysis is correct, Paulo. Linq is what provides IEnumerable in .NET.

cheers, Bill
Paulo Augusto Kunzel 6-May-15 10:06am    
Thanks guys,
After reading the documentation again, I noticed that it actually says "Extension methods" which matches Suvendu's answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900