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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:24:49+00:00 2026-06-15T18:24:49+00:00

In C++ it is done with using, and in C#? public class foo {

  • 0

In C++ it is done with “using”, and in C#?

public class foo
{
   public void print(string s) {...}
}

public class bar : foo
{
   // shadowing
   public void print(object o) {...}
}

How to promote foo.print, so foo.print and bar.print would be at the same “level” for compiler (for bar of course)?

Update 1

Originally I added a paragraph about common confusion between shadowing and overriding, but then I deleted it, because I thought it will be offensive to readers.

Shadowing is like overloading spanned over inheritance tree. Shadowing is NOT overriding.

Update 2

After shadowing foo.print is no longer taken into account when resolving the overloaded method print. Promoting foo.print would get it back into process — so when I call bar_object.print("hello") the method foo.print would be called.

  • 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-15T18:24:50+00:00Added an answer on June 15, 2026 at 6:24 pm

    In your concrete example, bar.print(object) indeed “shadows” the more specific foo.print(string):

    new bar().print("i am a string");
    

    This will call the method defined on bar, although the method on foo would have a parameter that matches the type better.
    What happens here is the following: The compiler finds a method on bar with the right name (“print”), the right number of parameters (1) and a parameter type to which the passed in parameter is convertable to (string can be converted to object).
    Because of this, there is no reason for the compiler to look further up the inheritance chain.

    As far as I am aware, there is no construct similar to C++’s using.
    If you want to use the method defined on the base class, you basically have three options:

    1. On the caller side: Convert the bar instance to foo:

      var bar = new bar();
      var foo = (foo)bar;
      foo.print("i am a string"); // Will call foo.print(string)
      
    2. On the calee side: Inside bar.print(object) check the type of the passed parameter:

      public void print(object o)
      {
          var s = o as string;
      
          if(s != null)
              base.print(s);
          else
          {
              // Other code.
          }
      }
      
    3. This will come the closest to the C++ using: Actually override or hide the original method in the derived class:

      public class bar : foo
      {
          public new void print(string s)
          {
              base.print(s);
          }
      
          public void print(object o)
          {
              // some code
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two classes. public class Foo { public Bar Args {
I'm wonder upto what level of hadoop programming can be done using PHP or
I have an object where I maintain a relationship to another object: public class
I've got the following: public abstract class Foo<T>{ //contents of Foo // ... public
class Foo1: public IFoo { public: template <class T> std::vector<T> foo() { return std::vector<T>();
I have a many-to-many relationship between Foo and Bar Here's a simplified Foo public
it can be done using javascript, but with CSS alone, is it possible to
This can certainly be done using a simple script and reading the database. I
I have an NSTableView that is done using the typical IB and data bindings.
I know this is possible to be done using javascript, and I presume I

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.