Click here to Skip to main content
15,886,036 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
public class MyObj
{
   public string MyObjName;
   
   public MyObj(string myObjName)
   {
      MyObjName = myObjName;
   }
}

public static Main(string[] args)
{
   List<MyObj> myObjs = new List<MyObj>();
   
   myObjs.Add(new MyObj("Obj1"));
   myObjs.Add(new MyObj("Obj2"));
   myObjs.Add(new MyObj("Obj3"));
   // etc...
}


If I wanted to find a specific MyObj e.g.(Obj100) in myObjs what is the fastest and most efficient way to go about this?
Posted
Updated 27-Dec-12 5:43am
v3

C#
    public class MyObj
    {
        public String MyObjName;
        public MyObj()
        {

        }
        public MyObj(String myObjName)
        {
            MyObjName = myObjName;
        }
        public void Hello()
        {
            Console.Write(MyObjName);
        }
    }
   
        static void Main()
        {
            List<myobj> myObjs = new List<myobj>();
            for (int i = 1; i < 101; i++)
            {
                myObjs.Add(new MyObj("Obj" + i));
            }


            object obj1 = Activator.CreateInstance(Type.GetType("Delegate.MyObj")) as String;
            MethodInfo mi = obj1.GetType().GetMethod("Hello");
            mi.Invoke(obj1, null);

        }
</myobj></myobj>
 
Share this answer
 
Please also consider the simplest option, using System.Collections.Generic.List<T>.Find:
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^].

In addition to a correct answer by Oleksandr Kulchytsky, please see my comments to this answer.

The thing about overriding of System.Object.Equals and System.Object.GetHashCode is not so trivial. I saw even pretty experienced developers who were confused. Short explanation is: it's important for some collection classes providing access by a key. For more detailed explanation, please see my past answer, recently updated:
Object.GetHashCode() Method in C#.Net[^].

This is interesting enough and very important to understand, please see.

Good luck,
—SA
 
Share this answer
 
v3
firs of all override method Equals and implement it logic in your class MyObj.

To find object in your's list you can laverage LINQ:

C#
myObjs.FisrtOrDefault(x=>x.MyObjName.Equals("obj100",StringComparisson.OrdinalIgnoreCase))
 
Share this answer
 
v3
Comments
d.allen101 27-Dec-12 11:54am    
question: is this similar to myObjs.Find()? if so i'm wondering if this is a foreach or for-loop behind the sense. do you know?
Oleksandr Kulchytskyi 27-Dec-12 11:58am    
Yep , it similar to First(), expect one thing in case if there is no such element with specified criteria , method will return null.
And yep again, under the hood of LINQ located old-plain foreach , for =))
d.allen101 27-Dec-12 12:10pm    
hey Oleksandr is there anyway I can avoid a linear search?
Oleksandr Kulchytskyi 27-Dec-12 14:22pm    
Hm , i think No.
d.allen101 27-Dec-12 12:13pm    
i know i can use a HastTable or Dictionary but the reason why i can't is because my List which is actually a BindingList is bound to dataGridView and neither HashTable or Dictionary support data binding
C#
for each (object obj in myObjs){
       if (obj.gettype() == typeof(MyObj)){
           //... do your thing obj is MyObj
       }
}
 
Share this answer
 
Comments
d.allen101 27-Dec-12 11:52am    
thx for the reply Adam, BUT what if I have 1000's of objects in my data structure. This won't be too efficient. I cant iterate over the entire collection it will be time consuming and resource intensive.
Adam R Harris 27-Dec-12 11:55am    
Depending on your framework version you could use LINQ like Oleksandr is suggesting.
d.allen101 27-Dec-12 12:02pm    
ok, i'm not too familiar with linq i'll have to do a little reading...so does the solution that Oleksandr provided get around iterating over the entire collection? i want some kind of hash lookup without having to provide the object itself as the lookup item. would be cool if i could use a property of the object.
Adam R Harris 27-Dec-12 12:04pm    
No, LINQ will just handle the iterating for you. Essentially there is no way to pull all your objects of a specific type out of a collection without actually iterating through the entire collection.
d.allen101 27-Dec-12 12:09pm    
so this won't be more efficient than the solution you provided (foreach)? is there anyway for me to get around O(N) Big O Notation?

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