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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:26:41+00:00 2026-06-15T01:26:41+00:00

I have a method which selects a distinct collection of objects, adds them to

  • 0

I have a method which selects a distinct collection of objects, adds them to a list and then returns the list.

Initially the method returned IEnumerable but as it returns a distinct collection I could add the distinct objects to a hashset and return ISet instead.

I appreciate that ISet makes the returned object modifiable (consumers could Add() to it) but I’m returning a new list anyway so I don’t think this affects me, as why do I care what the consumers do with the collection once they have it?

The question is, should I do this? Or should I just return IEnumerable and be happy that it only contains distinct elements (which is currently guaranteed by a test)

  • 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-15T01:26:42+00:00Added an answer on June 15, 2026 at 1:26 am

    As ever, with design, you have contradictory requirements at play here. The art of good design is choosing the best balance between these requirements for the product you are creating.

    One principal of interface design is that you consider the interface to be a contract or promise. Its obvious that, the less prescriptive a promise or contract is, the easier it is to keep. So, a return type of IEnumerable<T> means you are promising to return only a sequence of things. A “thighter” or more derived type would be more prescriptive and, a harder promise to keep.

    Reflectively, if you are doing work in your function to ensure that a return type is more dervied, then it seems wasteful to abandon that work by returning the less perscriptive parent type. If you already have an ISet<T>, defining a set of things, be explicit, return that and save the caller the work of the distinct check.

    In general, there is no simple answer, the right answer depends on the caller and your system. We have no knowledge of the caller in the general case. In the absence of evidence, we must assume nothing and take the tangible benefit of the weaker promise. So, in general, go with IEnumerable<T> it gives you greater flexibility in the furture with no known cost now.

    In the specific case, where we know the caller will benefit from the return of a set. This is you case where a bug is raised on the distinctness of the sequence. It makes sense to be explicit and return an ISet<T>, this will help prevent duplication of the distinct check. To add a caveat, this only makes sense if the data will be distinct for every possible call. You must ensure you will keep the promise for all callers, not just those who have raised the bug.

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

Sidebar

Related Questions

I have an extension method which takes in two list and compares them for
I have a method which returns single word as a String. I need to
I have a method which takes a list and do some processing on it
In short i have a method which selects a random operation and generates two
I have a list of all distinct account name prefixes (a-z) which I acquire
I have a datatable on which I am applying Select() method. After that I
I have this method which lets me translate the position of an object, animating
I have this method which leaks ~ 6KB : + (EInspectorFacilityInfo*) newWithNode: (CXMLNode*) node
I have a method which I am trying to call dynamically. That method has
I have a method which i want to convert to Extension Method public static

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.