C# - getting associated values from a class

Using C#, in the example below, how can I get the associated age by a name?

For example, I want to print the age of the Cat named Whiskers.

using System;
using System.Linq;
using cAlgo.API;
using System.Collections.Generic;

namespace cAlgo.Robots
{
    [Robot()]
    public class DemoExample : Robot
    {
        List<Cat> myCats = new List<Cat> 
        {
            new Cat 
            {
                Name = "Sylvester",
                Age = 8,
                LuckyNumbers = {1,2,3,4,5}
            },
            new Cat 
            {
                Name = "Whiskers",
                Age = 2,
                LuckyNumbers = {22,11,3,5,2}

            },
            new Cat 
            {
                Name = "Sasha",
                Age = 13,
                LuckyNumbers = {122,1,3,25,1}
            }
        };


        protected override void OnStart()
        {

            Print("The age of Whiskers is: " + myCats);

        }

        protected override void OnTick()
        {

        }

    }
}

public class Cat
{
    public string Name { get; set; }
    public int Age { get; set; }
    public List<double> LuckyNumbers = new List<double>();
}

Open in new window

LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Something like this should work.

foreach( cat theCat in myCats)
{
    Console.WriteLine("The age of {0} is : {1} ", theCat.Name, theCat.Age);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skijAuthor Commented:
I only want to find the age of the Cat with the Name of "Whiskers".

There could be hundreds of cats so looping with foreach seems like an inefficient way to do this.
0
AndyAinscowFreelance programmer / ConsultantCommented:
Looping is the only way (even if you don't do the loop yourself).  Use what is suggested, just add an extra test to check the name is what you want.


ps.  A dictionary collection (instead of a list) allows you to select an item rapidly without having to loop
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Ken ButtersCommented:
           foreach (Cat thisCat in myCats)
            {
                if (thisCat.Name.Equals("Whiskers"))
                {
                    Console.WriteLine("The age of Whiskers is: " + thisCat.Age);
                }

            }

Open in new window

0
skijAuthor Commented:
Thank you.  I have asked a related question here:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_28643971.html
0
Fernando SotoRetiredCommented:
Then this should do it.
 int age = myCats.Where (c => c.Name == "Whiskers" ).FirstOrDefault ().Age;
 Console.WriteLine("The age of Whiskers is: " + age.ToString());

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.