.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Collections >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

search object from the Generic list

Posted By:Praveen       Posted Date: May 11, 2009    Points: 10    Category: Collections    URL: http://www.dotnetspark.com  
 

In this Code we will see how to search object from the Generic list. This will be very useful when we use Generics and try to get the object depend on some condition.

Let's first create a class

using System;

///

/// Summary description for MyClass

///

public class MyClass

{

public MyClass()

{

}

public string Name;

public int ID;

}

Now we will see the how to search value from Generic list with below example

private void GetObejctFromGeneric()

{

List<MyClass> myClassList = new List<MyClass>();

//Create forst object

MyClass MyClass1 = new MyClass();

MyClass1.ID = 1;

MyClass1.Name = "FName";

myClassList.Add(MyClass1); //Add to List

//Craete aother object

MyClass1 = new MyClass();

MyClass1.ID = 2;

MyClass1.Name = "LName";

myClassList.Add(MyClass1); // Add to List

//Search Implementation

MyClass myClass = myClassList.Find(delegate(MyClass myclassToFind)

{

return myclassToFind.ID == 3; //Search condition goes here

});

}

Happy Coding
Praveen!!!


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend