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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:21:47+00:00 2026-05-26T20:21:47+00:00

I have an abstract immutable base class that defines enforces child classes to be

  • 0

I have an abstract immutable base class that defines enforces child classes to be initialized, hence the abstract calls instead of an interface:

public abstract BaseLookup<TPoint, TItem>
{
    protected IEnumerable<TItem> items = null;

    protected BaseLookup(IEnumerable<TItem> items)
    {
        this.items = items;
        this.Initialize();
    }

    public abstract void Initialize();

    // problem deciding which one

    // either implementing a method...
    public abstract TItem GetItem(TPoint point);

    // ...or assigning a method
    public Func<TPoint, TItem> GetItem { get; protected set; }
}

GetItem execution has to be as fast as possible. During initialization stage I have to check initial items and decide what GetItem method should do. It can be one of many implementations based on this set of items.

Since GetItem method has to be as fast as possible it seems much better to have it defined as a property and assign it a straight forward branch-less lambda expression. But using above definition child classes are not forced to set any values to it so implementers may create an invalid child class. Defining an abstract accessor on the property would force them to implement the property which is semantically the same as implementing a method. This doesn’t enforce property assignment.

But if I implement it as an overridden abstract method that particular method will need to include all those branches that branch based on items. This means that these branches would get evaluated every time I’d call the method hence making it slow(er).

What I’m actually looking for is a way to force child class implementers to set GetItem property.

How am I supposed to do that?

Also take into consideration that this class will get initialized once and then be used many many times. Used as in calling the GetItem method.

A simplified example class (using property)

public class IteratorLookup<TPoint, TItem> : BaseLookup<TPoint, TItem>
{
    private TItem single = null;

    public IteratorLookup(IEnumerable<TItem> items) : base(items);

    public override void Initialize()
    {
        if (this.items != null && this.items.Count > 0)
        {
            if (this.items.Count > 1)
            {
                this.GetItem = point => this.items[this.GetIndex(point)];
            }
            else
            {
                this.GetItem = irrelevant => this.single;
            }
        }
        else
        {
            this.GetItem = irrelevant => null;
        }
    }

    private int GetIndex(TPoint point) { ... }
}

A simplified example class (using a method)

public class IteratorLookup<TPoint, TItem> : BaseLookup<TPoint, TItem>
{
    private TItem single = null;

    public IteratorLookup(IEnumerable<TItem> items) : base(items);

    public override void Initialize()
    {
        // implementing minor speed up
        if (this.items != null && this.items.Count == 1)
        {
            this.single = items[0];
        }
    }

    public override TItem GetItem(TPoint point)
    {
        if (this.items != null && this.items.Count > 0)
        {
            if (this.items.Count > 1)
            {
                return this.items[this.GetIndex(point)];
            }
            return this.single;
        }
        return null;
    }

    private int GetIndex(TPoint point) { ... }
}
  • 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-26T20:21:47+00:00Added an answer on May 26, 2026 at 8:21 pm

    Since GetItem method has to be as fast as possible it seems much better to have it defined as a property and assign it a straight forward branch-less lambda expression

    What makes you think it will be faster than a method? The abstract method is the way to go here. It’s a more natural approach, and it doesn’t have the issue that the property might not be initialized.

    But if I implement it as an overridden abstract method that particular method will need to include all those branches that branch based on items. This means that these branches would get evaluated every time I’d call the method hence making it slow(er).

    Not sure what you mean by that… what prevents you from putting in the GetItem method the same code you would have put in a Func<TPoint, TItem> in the GetItem property?

    As a side note, you should probably think twice about calling a virtual method (Initialize) in the constructor, it can lead to unexpected issues: the base constructor is executed before the derived constructor, but it’s always the most derived implementation of Initialize that will be called; so if the derived implementation of Initialize relies on things that are initialized in the derived class constructor, it will fail because they won’t be initialized yet.

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

Sidebar

Related Questions

I have an abstract base class which acts as an interface. I have two
I have one main interface and an abstract class implementing all derivable methods (that
I have an abstract class that other classes are inheriting this. public abstract class
Imagine I have abstract base class Shape , with derived classes Circle and Rectangle
I have an abstract base class from which many classes are derived. I want
I have an abstract base class with a TcpClient field: public abstract class ControllerBase
I have an abstract base class and derived class: type TInterfaceMethod = class public
I have an abstract base class and I want to declare a field or
I have an abstract class (I know that it will not compile this way,
I have a abstract super class called RealAlgebraicNumber and two inherited classes called IntervalRepresentation

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.