Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8215713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:52:42+00:00 2026-06-07T11:52:42+00:00

I’m learning C# from a book and i’m expanding on an example in an

  • 0

I’m learning C# from a book and i’m expanding on an example in an effort to better understand the syntax.

I’m trying to use the following code to cycle through a collection of objects and pick out only certain ones so I can load them into a separate array. I’m struggling with this particular line right now:

if (animalCollection[i].Equals(Chicken))

Here is the complete code for Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ch11Ex02
{
class Program
    {
    static void Main(string[] args)
        {
        Animals animalCollection = new Animals();
        animalCollection.Add(new Cow("Jack"));
        animalCollection.Add(new Chicken("Vera"));
        animalCollection.Add(new Chicken("Sally"));

        Animal[] birds = new Animal[2];
        for (int i = 0; i < animalCollection.Count; i++)
            {
            if (animalCollection[i].Equals(Chicken))
                birds[i] = animalCollection[i];
            }

        foreach (Animal myAnimal in animalCollection)
            {
            myAnimal.Feed();
            }
        Console.ReadKey();
        }
    }
}

My goal is to load only object types Chicken into a new array called birds.

here is the code for class Animal:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ch11Ex02
{
public abstract class Animal
    {
    protected string name;

    public string Name
        {
        get
            {
            return name;
            }
        set
            {
            name = value;
            }
        }

    public Animal()
        {
        name = "The animal with no name";
        }

    public Animal(string newName)
        {
        name = newName;
        }

    public void Feed()
        {
        Console.WriteLine("{0} has been fed." , name);
        }

    internal bool equals()
        {
        throw new NotImplementedException();
        }
    }
}

and here is the code for class Chicken:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ch11Ex02
{
public class Chicken : Animal
    {
    public void LayEgg()
        {
        Console.WriteLine("{0} has laid an egg." , name);
        }
    public Chicken(string newName): base(newName)
        {
        }
    }
}

and here is the code for class Animals:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ch11Ex02
{
public class Animals : CollectionBase
    {
    public void Add(Animal newAnimal)
        {
        List.Add(newAnimal);
        }

    public void Remove(Animal newAnimal)
        {
        List.Remove(newAnimal);
        }

    public Animals()
        {
        }

    public Animal this[int animalIndex]
        {
        get
            {
            return (Animal)List[animalIndex];
            }
        set
            {
            List[animalIndex] = value;
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:52:43+00:00Added an answer on June 7, 2026 at 11:52 am

    Fundamentals

    To determine whether an object is of a given type, you can use typeof or is

    if (typeof(someObject) == typeof(Chicken))
    

    or

     if (someObject is Chicken)
    

    so specifically in your case

    if (animalCollection[i].Equals(Chicken))
    

    becomes

    if (typeof(animalCollection[i]) == typeof(Chicken))
    

    or

    if (animalCollection[i] is Chicken)
    

    You can also determine an object’s type like this

    Type t = animalCollection[i].GetType();
    

    The Fast Way

    Now that I have covered how this works at a basic level, here’s a way to accomplish the same in one line, using Linq

    var chickens = animals.OfType<Chicken>().ToArray();
    

    By the Way

    If you then wanted to get the type name as a string, you could do this

    string typeName = t.FullName;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.