A
A
Alex2015-11-05 23:17:46
Programming
Alex, 2015-11-05 23:17:46

How to get a class object from a list by the value of one of the variables?

Hello! There is a list of class objects and the class itself:

class Program
    {
        static void Main(string[] args)
        {
            List<Test> TestList = new List<Test> {
               new Test(1,2),
               new Test(1,1),
               new Test (1,3),
               new Test(1,6)           
   };
      }
}

 class Test
    {
        private int Var1, Var2;
        public Test(int _var1,int _var2)
        {
            Var1 = _var1;
            Var2 = _var2;
        }
    }

It is necessary to get an object from the list, which, for example, has Var2==3. What is the best way to do this?
You can of course use For Each, but maybe there is an option without enumeration? Apologies in advance if this is a noob question.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
amf1k, 2015-11-05
@OXDemager

Linq like, TestList.where(o => o.Var2 == 3).FirstorDefault();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question