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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:35:48+00:00 2026-06-15T21:35:48+00:00

I have the following base and derived (partial, for sake of brevity) classes: class

  • 0

I have the following base and derived (partial, for sake of brevity) classes:

class Base {
     public abstract int[] someArray { get; }
}

class Derived : Base {
     private readonly static int[] _someArray = new int[] { 1,2,3,4 };
     public override int[] someArray { 
         get {
              return _someArray;
         }
     }
}

What I would like now, is put the new int[] { 1,2,3,4 } in the return part of the getter. But, that would create a new array every time the getter is called.

Is it possible to directly return some kind of object, which stays the same for all objects of class Derived ?

Something along the lines of (I know this is invalid C#):

  get {
        return (int[]) { 1, 2, 3, 4 };
  }
  • 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-15T21:35:49+00:00Added an answer on June 15, 2026 at 9:35 pm

    If you only want to put the new int[] { 1,2,3,4 } part into the getter, that’s easy…

     private static int[] _someArray = null;
     public override int[] someArray { 
         get {
              return _someArray ?? (_someArray = new int[] { 1,2,3,4 });
         }
     }
    

    You’d lose the readonly then, though.

    UPDATE: Generics solution

    Or you could abuse the features of generics for that:

    // Of course you still want to use the non-generic class Base
    abstract class Base {
        public abstract int[] someArray { get; }
    }
    
    abstract class Base<T> : Base where T: Base<T> {
         // Will be created once for every T, i.e. every derived class.
         private static int[] _someArray;
    
         public override int[] someArray {
            get { return _someArray ?? (_someArray = CreateArray()); }
         }
    
         protected abstract int[] CreateArray();
    }
    
    sealed class Derived : Base<Derived> {
         protected sealed override int[] CreateArray() { 
             return new int[] { 1,2,3,4 };
         }
    }
    
    sealed class DerivedB : Base<DerivedB> {
        protected sealed override int[] CreateArray() {
            return new int[] { 2,3,4,5 };
        }
    }
    

    Note that this only works for one inheritance level, so i sealed some stuff 🙂

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

Sidebar

Related Questions

I have the following base, middle and derived classes below:: public class Base {
I have the following simple class class base { public: int x; base &set(int
Consider the following program: class Base { private: int m_nID; public: Base() { m_nID
I have following class base { }; class derived : public base { public:
I have the following base class: abstract class Base { public abstract object Var
Say I have the following: class Base { public Base (int n) { }
I have the following setup, abridged for brevity: public abstract class AuditableEntity { public
Suppose I have the following (trivially simple) base class: public class Simple { public
I have the following code: public partial class Form1 : Form { public BindingList<Class>
Suppose that we have the following base and derived classes: #include <string> #include <iostream>

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.