Click here to Skip to main content
15,794,850 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,
I write a code in console Application for search from the stack. I made the user defined data type of Person in user defined data type there are attributes of Person are name,age,address.
Now i want to search the data by using name and search it from the stack.

What I have tried:

public bool search_Person(Stack<Person>mystack,string name)
       {
           foreach(Person pr in mystack)
           {
               if (mystack==pr.name)
               {
                   return true;
               }
           }
           return false;
       }
   }
Posted
Updated 17-Oct-19 21:13pm
Comments
Richard Deeming 18-Oct-19 10:58am    
NB: Stack<T> implements IEnumerable<T>, so you can use LINQ:
Introduction to LINQ Queries (C#) | Microsoft Docs[^]
public bool search_Person(Stack<Person> mystack, string name)
{
    return mystack.Any(pr => pr.name == name);
}

1 solution

Change this:
if (mystack==pr.name)
To this:
if (name == pr.name)
 
Share this answer
 
Comments
CPallini 18-Oct-19 2:56am    
5.

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