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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:05:57+00:00 2026-05-12T08:05:57+00:00

Okay, two alternatives, but before I begin, you need to know this: public abstract

  • 0

Okay, two alternatives, but before I begin, you need to know this:

public abstract class GatewayBase { ... }

public class Gateway : GatewayBase { ... }

Alternative #1

public abstract class ModelBase
{
    public GatewayBase GatewayBase { get; private set; } // property named GatewayBase

    public ModelBase(GatewayBase gateway)
    {
        GatewayBase = gateway;
    }
}

public class Model : ModelBase
{
    public Gateway Gateway { get; private set; } // property named Gateway

    public Model(Gateway gateway)
        : base(gateway)
    {
        Gateway = gateway;
    }
}

Alternative #2

public abstract class ModelBase
{
    public GatewayBase Gateway { get; private set; } // property named Gateway

    public ModelBase(GatewayBase gateway)
    {
        Gateway = gateway;
    }
}

public class Model : ModelBase
{
    public new Gateway Gateway { get; private set; } // property named Gateway plus "new" modifier

    public Model(Gateway gateway)
        : base(gateway)
    {
        Gateway = gateway;
    }
}

Discussion:

With Alternative #1, the concrete class Model can “see” two versions of Gateway. One is called GatewayBase and the other is called just Gateway, but they both contain the exact same instance. With Alternative #2, technically, there are still two versions of Gateway but one hides the other, so there is effectively only one (unless you bypass it using base.Gateway). I like that Alternative #2 lets me call the property Gateway wherever I am, because it gets used a lot in both the base and concrete classes and it’s a short but clear name. Still, I have some hesitation about using the new modifier in this way. Is this really a legitimate scenario for hiding a property?

Which would you choose and why?

Or feel free to suggest other alternatives.

Thanks.

EDIT:

I should mention that GatewayBase and ModelBase are in a dependent assembly, so they don’t know anything about Gateway and Model. However, Gateway and Model of course know about GatewayBase and ModelBase.

  • 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-12T08:05:57+00:00Added an answer on May 12, 2026 at 8:05 am

    Option 2 looks cleaner, but don’t make a separate backing property, wrap the existing base class’ property by casting it up from GatewayBase to Gateway. This way you won’t have ambiguities about the Gateway used: it’s always the same, just from a different perspective:

    public abstract class ModelBase
    {
        public ModelBase(GatewayBase gateway)
        {
            this.Gateway = gateway;
        }
    
        public GatewayBase Gateway { get; private set; }
    }
    
    public class Model : ModelBase
    {
        public Model(Gateway gateway)
            : base(gateway)
        {
        }
    
        public new Gateway { get { return (Gateway) base.Gateway; } }
    }
    

    You can also use generics to keep things a little more flexible for different types of gateways (a bit like IEnumerable<T>). The problem with generics is that you can’t cast one C<X> to another C<Y> (well, you can, sometimes, in 4.0). The cleanest way to solve that, is to introduce a non-generic interface, which you implement explicitly on your generic class. This way, it’s hidden from view when you’re talking to generic instances, but you can still mix your C<X>’s and C<Y>’s.

    public interface IModel
    {
        GatewayBase Gateway { get; }
    }
    
    public abstract class ModelBase<TGateway> : IModel
        where T : GatewayBase
    {
        public ModelBase(TGateway gateway)
        {
            this.Gateway = gateway;
        }
    
        public TGateway Gateway { get; private set; }
    
        GatewayBase IModel.Gateway { get { return this.Gateway; } }
    }
    
    public class Model : ModelBase<Gateway>
    {
        public Model(Gateway gateway)
            : base(gateway)
        {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay so part two on this. I restructured the code on this but now
Okay, so I got two classes. class a{ public: a(){}; void print(){cout << hello};
Okay, I have two OperationContracts and MessageContracts, like this: [OperationContract] OperationResult OperationOnSingleItem(Input input) [OperationContract]
Okay, we know that the following two lines are equivalent - (0 == i)
Okay, so this seems simple, but I can't think of a straightforward solution; Basically
Okay, so here's the issue. I have two apps that need to package up
Okay, so let's say I have a class file called Orange, and then two
Okay, I need some help here. This is the same old "can't use an
Okay, so far I can select two tables using mysql but I cant select
Okay, i have this question in one regarding threads. there are two unsynchronized threads

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.