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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:37:34+00:00 2026-05-13T10:37:34+00:00

I’m writing a method in C# (2.0) that has to return a collection of

  • 0

I’m writing a method in C# (2.0) that has to return a collection of simple objects. Normally I would do something like this:

class MyWidget
{
    struct LittleThing
    {
        int foo;
        DateTime bar;
    }

    public IList<LittleThing> LookupThings()
    {
        // etc.
    }
}

However, I have to declare this method in an interface. The caller doesn’t get to see MyWidget, only an IWidget interface. The above setup doesn’t work in that situation, because C# does not allow defining types inside an interface. What is the proper or best way to do such a declaration?

The straighforward thing I thought of is to simply declare LittleThing outside of the interface. That doesn’t seem great, for a couple of reasons. One: it is only ever used by that single method in that single class, so it doesn’t seem that LittleThing should be an independent type just floating around by itself. Two: if similar methods wind up being written for other classes, they will be returning different kinds of data (for good design reasons), and I don’t want to clutter the namepace with a ton of similar-named structs that differ only slightly from each other.

If we could upgrade our version of .Net, I would just return a Tuple<>, but that’s not going to be an option for some time yet.

[Edited to add: The small object does need to contain more than two fields, so KeyValuePair<K,V> won’t quite cut it.]

[Edited to add further: IWidget is implemented by only one class, Widget. I think it weird to have an interface for only one class, but this was done to satisfy an old coding policy that required the contract to always be in a separate assembly from the implementation. Said policy has now gone away, but we haven’t the resources to refactor the entire application and remove all the unnecessary interfaces.]

What’s the best practice?

  • 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-13T10:37:35+00:00Added an answer on May 13, 2026 at 10:37 am

    If we could upgrade our version of .Net, I would just return a Tuple<>, but that’s not going to be an option for some time yet.

    Why wait? It’s not like a tuple is a complicated thing. Here’s the code for a 3-tuple.

    public struct Tuple<TItem1, TItem2, TItem3>
    {
        public Tuple(TItem1 item1, TItem2 item2, TItem3 item3)
        {
            this = new Tuple<TItem1, TItem2, TItem3>();
            Item1 = item1;
            Item2 = item2;
            Item3 = item3;
        }
    
        public static bool operator !=(Tuple<TItem1, TItem2, TItem3> left, Tuple<TItem1, TItem2, TItem3> right)
        { return left.Equals(right); }
    
        public static bool operator ==(Tuple<TItem1, TItem2, TItem3> left, Tuple<TItem1, TItem2, TItem3> right)
        { return !left.Equals(right); }
    
        public TItem1 Item1 { get; private set; }
        public TItem2 Item2 { get; private set; }
        public TItem3 Item3 { get; private set; }
    
        public override bool Equals(object obj)
        {
            if (obj is Tuple<TItem1, TItem2, TItem3>)
            {
                var other = (Tuple<TItem1, TItem2, TItem3>)obj;
                return Object.Equals(Item1, other.Item1)
                    && Object.Equals(Item2, other.Item2)
                    && Object.Equals(Item3, other.Item3);
            }
            return false;
        }
    
        public override int GetHashCode()
        {
            return ((this.Item1 != null) ? this.Item1.GetHashCode() : 0)
                 ^ ((this.Item2 != null) ? this.Item2.GetHashCode() : 0)
                 ^ ((this.Item3 != null) ? this.Item3.GetHashCode() : 0);
        }
    }
    

    As you can see, it’s no big deal. What I’ve done on my current project is implement 2, 3 and 4-tuples, along with a static Tuple class with Create methods on it, which exactly mirror the .NET 4 tuple types. If you’re really paranoid you can use reflector to look at the dissassembled source code for the .NET 4 tuple, and copy it verbatim

    When we eventually upgrade to .NET 4, we’ll just delete the classes, or #ifdef them out

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I know there's a lot of other questions out there that deal with this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display

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.