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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:32:55+00:00 2026-05-25T09:32:55+00:00

in the following code I define an interface, an abstract base class with a

  • 0

in the following code I define an interface, an abstract base class with a method that prints “foo”, a class that implements both, and an extension method on the interface with a signature equal to a method in the abstract base class that prints “bar”. when I run this sample, why is “bar” printed instead of “foo”? and if applicable, what’s the morale behind this language design choice?

public interface ISomething
{}

public abstract class SomethingElse
{
    public void foo()
    {
        Console.WriteLine("foo");
    }
}

public class DefinitelySomething : SomethingElse, ISomething
{}

public static class ISomethingExtensions
{
    public static void foo(this ISomething graphic)
    {
        Console.WriteLine("bar");
    }
}

class Program
{
    static void Main(string[] args)
    {
        ISomething g = new DefinitelySomething();
        g.foo();
    }
}
  • 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-25T09:32:56+00:00Added an answer on May 25, 2026 at 9:32 am

    Because the variable is declared as ISomething.

    The instance method is not known until run-time however the method overload resolution is at compile time. There’s no guarantee that the instance actually has a suitable method. In you particular example it has but that’s from a type safety perspective an irrelevant coincidence. The important type at the line g.foo() is the type that g is declared as not the run time type

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

Sidebar

Related Questions

Possible Duplicate: Why would one declare a Java interface method as abstract? The following
The following code gives me the warning Contract class 'FooContracts' should be an abstract
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
In Java, it is very easy to code the following design: public abstract class
I have the following base abstract class defined as: public abstract class BaseObject<T> :
I have the following C code: #define PRR_SCALE 255 ... uint8_t a = 3;
The following code works fine #define open { #define close } #include<stdio.h> #define int
I have the following code snippet: #ifdef DO_LOG #define log(p) record(p) #else #define log(p)
Why does the following code give me a segmentation fault? #define MAXROWS 10 #define
I have the following code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> #define NUM_THREADS 100

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.