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 4019772
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:08:46+00:00 2026-05-20T10:08:46+00:00

Question: Have I got this example backwards? Is the reason to return Interface so

  • 0

Question: Have I got this example backwards?

Is the reason to return Interface so that:

Try1:

public class Thing
{
    public string name { get; set; }
    public int age { get; set; }

    public IList<Thing> giveMeAllThings() 
    {
        IList<Thing> listOfThings = new List<Thing>();
        Thing thing1 = new Thing { name = "phone", age = 3 };
        Thing thing2 = new Thing { name = "waterbottle", age = 2 };
        Thing thing3 = new Thing { name = "pinecone", age = 17 };
        listOfThings.Add(thing1);
        listOfThings.Add(thing2);
        listOfThings.Add(thing3);

        return listOfThings;
    }
}

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void UnitTestThings()
    {
        Thing thing = new Thing();
        IEnumerable<Thing> listOfThings = thing.giveMeAllThings(); 

        Assert.AreEqual(3, listOfThings.Count()); // linq count
        Assert.IsTrue(listOfThings.Any(t => t.name == "phone" && t.age == 3));
        Assert.IsTrue(listOfThings.Any(t => t.name == "waterbottle" && t.age == 2));
        Assert.IsTrue(listOfThings.Any(t => t.name == "pinecone" && t.age == 17));
    }
}

try2. I figure out that as I only need IEnumerable then I can just return that.

public IEnumerable<Thing> giveMeAllThings() 
    {
        IList<Thing> listOfThings = new List<Thing>();
        Thing thing1 = new Thing { name = "phone", age = 3 };
        Thing thing2 = new Thing { name = "waterbottle", age = 2 };
        Thing thing3 = new Thing { name = "pinecone", age = 17 };
        listOfThings.Add(thing1);
        listOfThings.Add(thing2);
        listOfThings.Add(thing3);

        foreach (Thing t in listOfThings)
            yield return t;
    }

Edit:
So if I need a List in one method but not another, then the most ‘General’ interface possible to return is an IList:

public IList<Thing> giveMeAllThings() 
    {
        IList<Thing> listOfThings = new List<Thing>();
        Thing thing1 = new Thing { name = "phone", age = 3 };
        Thing thing2 = new Thing { name = "waterbottle", age = 2 };
        Thing thing3 = new Thing { name = "pinecone", age = 17 };
        listOfThings.Add(thing1);
        listOfThings.Add(thing2);
        listOfThings.Add(thing3);

        return listOfThings;
    }

[TestMethod]
    public void UnitTestThings()
    {
        Thing thing = new Thing();
        IEnumerable<Thing> listOfThings = thing.giveMeAllThings(); 
        Assert.AreEqual(3, listOfThings.Count()); // linq count
    }

    [TestMethod]
    public void UnitTestThingsWhereINeedAnActualList()
    {
        Thing thing = new Thing();
        IList<Thing> listOfThings = thing.giveMeAllThings();
        Assert.AreEqual(3, listOfThings.Count); // List count
    }
  • 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-05-20T10:08:47+00:00Added an answer on May 20, 2026 at 10:08 am

    Is the reason to return Interface so that:

    The main reason to do this is that, by returning the most “general” purpose interface possible, you have more flexibility in changing your internal implementation to something “better” (ie: more efficient, simpler code, whatever the criteria may be) later, without breaking your public API.

    For example, if you switch from IList<T> to IEnumerable<T>, you could switch your code around to use iterators (yield return) instead of a collection. You may decide that a different collection (instead of List<T>) works better in your code, and switch to it. If that second collection didn’t implement IList<T>, it would require you to change your public API.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The responses I've got to this question have solved the problem I had in
Question: I have a question that is apparently not answered by this already-asked Bash
Question We have a large number of xml configuration files that we want merged
This is a question I have wondered about for quite some time, yet I
From a previous question I have seen that the CLR has workstation and server
I have got stuck with a question I have just been helped on -
My girlfriend got this question in an interview, and I liked it so much
I've got this small question - given a bitmask of weekdays (e.g., Sunday =
I've got this HTML form where I have multiple input elements: text, radio, and
Several questions about functional programming languages have got me thinking about whether XSLT is

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.