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

  • Home
  • SEARCH
  • 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 8761375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:16:14+00:00 2026-06-13T15:16:14+00:00

Given a class hierarchy with a base class B and a subclass S :

  • 0

Given a class hierarchy with a base class B and a subclass S:

class B { }
class S : B { }

I can assign a S to an B with an implicit conversion:

B b = new S();

If I wanted to downcast this back to a S I would have to do this explicitly:

B b = new S();
...
S s = (S)b;

Now, my understanding is we can guarantee that there is always assignment compatibility going from S to B, so we will never have to perform an explicit upcast in the following way:

S s = new S();
B b = (B)s; // or
B b2 = s as B;

Is this assertion correct, or as the question states Do I Ever Have to Perform An Explicit Upcast?

  • 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-13T15:16:15+00:00Added an answer on June 13, 2026 at 3:16 pm

    If you have

    class B { }
    class S : B { }
    class T : B { }
    

    and you try to do something like

    var b = myBool ? new S() : new T();
    

    this won’t compile unless you explicitly cast either the S or the T instance (or both) to B.

    The same goes for anonymous methods; this

    new bool[0].Select(b => 
        { if (b) { return new S(); } else { return new T(); } });
    

    won’t compile without casts (or specifying Select<bool, B> but this might not be possible, for example if your source is an anonymous type).

    Also, if you have

    class B { public void DoSomething() { } }
    class S : B { public new void DoSomething() { } }
    

    then

    S s = new S();
    s.DoSomething();       // calls the S version
    ((B)s).DoSomething();  // calls the B version
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

given a Python class hierarchy, say class Base: def method1 def method2 def method3
The essence of the problem is, given a class hierarchy like this: class A
Given the following hierarchy class Content < ActiveRecord::Base end class Page < Content end
Given a class hierarchy that looks like this: public class Vehicle { private String
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
Given a class hierarchy where the base class defines a recursive self-type: abstract class
Given this: Interface IBase {string X {get;set;}} Interface ISuper {string Y {get;set;}} class Base
Given the following class hierarchy, I would like Foo to be serialized differently depending
Given a .proto that looks like this: message Base { string Dummy = 1
I have this class hierarchy: public interface ISR { } public interface ICU {

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.