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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:13:14+00:00 2026-06-04T13:13:14+00:00

Let say we have Class A and Class B. ClassB extends Class A. (ClassB

  • 0

Let say we have Class A and Class B. ClassB extends Class A. (ClassB : ClassA)

Now let’s say that whenever I instantiate ClassB, I’d like to Run some Random code and only then call “base” to reach ClassA constructor.

Like:

class ClassA
{
    public ClassA()
    {
        Console.WriteLine("Initialization");
    }  
}

class ClassB : ClassA
{
    public ClassB() //: base() 
    {
        // Using :base() as commented above, I would execute ClassA ctor before                                                         //          Console.WriteLine as it is below this line... 
        Console.WriteLine("Before new");
        //base() //Calls ClassA constructor using inheritance
        //Run some more Codes here...
    }
}

In the programming language I usually work with, I can do that, by simply calling super() after Console.WriteLine(); But I cant make it in C#. Is there any other syntax or other way to do that?

  • 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-04T13:13:15+00:00Added an answer on June 4, 2026 at 1:13 pm

    There’s a hacky way of doing it using an instance variable initializer:

    using System;
    
    class ClassA
    {
        public ClassA()
        {
            Console.WriteLine("Initialization");
        }  
    }
    
    class ClassB : ClassA
    {
        private readonly int ignoreMe = BeforeBaseConstructorCall();
    
        public ClassB()
        {
        }
    
        private static int BeforeBaseConstructorCall()
        {
            Console.WriteLine("Before new");
            return 0; // We really don't care
        }
    }
    
    class Test
    {
        static void Main()
        {
            new ClassB();
        }    
    }
    

    The less hacky way of doing it is to rethink how you construct a ClassB to start with. Instead of having clients call the constructor directly, provide a static method for them to call:

    public static ClassB CreateInstance()
    {
        Console.WriteLine("Before initialization stuff");
        return new ClassB();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a manager that looks something like this: public class CustomerManager
Let's say I have a defined ClassA. ClassB extends ClassA and there's a Movie
Let's say I have: public class Components<T> extends TupleList<Class<T>, String> { private static final
Let's say I have a class Foo with some primitive instance variables. I initialize
Let's say I have a base class named Entity . In that class, I
Let's Say I Have This Class: package{ import flash.display.Sprite; public class Main extends Sprite{
Let's say I have ordinary *Type class: class LocationType extends AbstractType { /** *
I have a library project which contains an abstract class, let's say ClassA .
Let say I have a Transformation class that extending a CommonDomain class. and I
I have this class, let's say, Foo . It extends JFrame and is a

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.