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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:33:58+00:00 2026-05-15T13:33:58+00:00

In what scenarios would somebody pass (or receive) an interface as a parameter? Is

  • 0

In what scenarios would somebody pass (or receive) an interface as a parameter? Is it really a useful thing or just a fancy way of doing something?

  • 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-15T13:33:58+00:00Added an answer on May 15, 2026 at 1:33 pm

    It’s an extremely useful thing.

    Take any of the LINQ extension methods, for instance. They don’t care what’s passed to them, as long as it implements IEnumerable<T>. The idea is that they can all be applied to anything that you can enumerate over using a foreach loop.

    Imagine how pointlessly restrictive it would be if they all required you to pass T[] arrays, or List<T> objects, for example.


    Here’s just one very trivial illustration. Let’s pretend the LINQ extensions don’t exist (which is actually a real possibility if I’m using .NET 2.0) and I want to write a Sum method.

    I could write it like this:

    public static double Sum(List<double> values)
    {
        double sum = 0.0;
        foreach (double value in values)
        {
            sum += value;
        }
        return sum;
    }
    

    That’s all well and good, but notice something here: I wrote the method to take a List<double>, which is a class that has far more functionality than this code depends on. Where does it use Insert? Where does it use RemoveAt? FindAll? Sort? Nope, none of that is required. So is it really necessary that this method get passed a List<double>?

    Moreover, say I have a double[]. Theoretically, I should be able to pop that right in as the values parameter, since all I’m doing is enumerating over it using a foreach; but since I’ve typed values as List<double>, to pass a double[] to my Sum method I’d have to do this:

    double sum = Sum(new List<double>(myArray));
    

    That’s just a completely unnecessary new object I’ve constructed simply to call code that really should’ve been able to handle my original object in the first place.

    By writing methods that take interfaces as parameters, you make your code more flexible and more powerful, and you avoid imposing inappropriate restrictions (give me an X, even though I could just as easily do this with a Y) on calling code.

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

Sidebar

Related Questions

What are some scenarios where MultiView would be a good choice? The MultiView control
How would the following scenario best be implemented: There is a standardized user interface
How would you test this scenario? I've just started looking into NHibernate and having
There are few scenarios which i can expect during my execution, I would like
Under what scenarios would one want to connect to a host other than localhost?
Which of theses scenarios would be faster? Scenario 1: foreach (var file in directory.GetFiles())
Could somebody please help me understand the concept of 'impersonation'? The way I understand
What would you suggest for the following scenario: A dozen of developers need to
I would like to implement a producer/consumer scenario that obeys interfaces that are roughly:
What all would be the requirements for the following scenario: A GSM modem connected

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.