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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:34:56+00:00 2026-05-31T04:34:56+00:00

In general scenario, the interface or abstract class is often the appropriate decision, am

  • 0

In general scenario, the interface or abstract class is often the appropriate decision, am I right?

But in some cases, it looks like the concrete class is better. For instance,

public string Replace(string old, string new)

The Replace method of String returns a concrete class. (It’s just an example, although String doesn’t implement any interfaces.)

My question is

  1. When should I return an interface, and when should I return a concrete class?

  2. Is it a part of program to an interface, not an implementation for returning an interface?

  • 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-31T04:34:57+00:00Added an answer on May 31, 2026 at 4:34 am

    It depends.

    I’ve seen this question asked a couple of times, and here’s a nice example to illustrate the “it depends” answer.

    Consider the following class:

    public class MyClass
    {
        public static IEnumerable<int> Test()
        {
            return new List<int> { 2, 3, 4 };
        }
    
        public static List<int> Test2()
        {
            return new List<int> { 2, 3, 4 };
        }
    }
    

    Test returns an IEnumerable and Test2 returns a concrete implementation of the IEnumerable interface (List in that case). What is the best method? Test or Test2?

    Actually, both are semantically different:

    • As Test only returns an IEnumerable, it implies that it’s a part of the method contract that the developer uses the returned object in an enumeration (foreach).
    • As Test2 returns a List instance, it allows the user to access to the objects of the List by index. It’s a totally different utilization of the returned object.

    private static void Main(string[] args)
    {
        foreach (var z in MyClass.Test())
        {
            Console.WriteLine(z);
        }
    
        var f = MyClass.Test2()[0];
    
        Console.ReadKey();
    }
    

    If you expect the developer to use the returned object in an enumeration only, then you could use the interface as return type. If you expect the developer to use methods/properties of the concrete implementation of the interface (in the above example, access to object by index), then you could return a concrete type.

    Also remember that sometimes you have no choice. For example, if you want to expose a public collection that should be used for a Silverlight binding, then you should return ObservableCollection<T>, not IEnumerable<T>, because the binding system actually needs the method/properties/behavior of the ObservableCollection class (IEnumerable would be not sufficient for the binding to work).

    What you should avoid is a method that returns IEnumerable<T> and that is used with ToList() every time.

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

Sidebar

Related Questions

Possible Duplicate: Interface vs Abstract Class (general OO) Following are the confusions which I
I'd like to define an interface and abstract base class for a set of
Possible Duplicate: Interface vs Abstract Class (general OO) Hi guys, I decided to dig
general question is i like to build logger class that writes to single log
In general, I have the following scenario: Fetch product and its related data from
I'm try to figure out how to handle the following scenario. In general, i
For general protocol message exchange, which can tolerate some packet loss. How much more
If one is designing an interface like ILookup (for simplicity, assume it's simply ICheckIfContained
I have a common scenario that I am looking for some guidance from people
I have a general JavaScript question. I'll give you my scenario and then ask

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.