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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:10:29+00:00 2026-06-03T01:10:29+00:00

int a, b, c; Constructor() { a = 5; b = 10; c =

  • 0
int a, b, c;

Constructor()
{
    a = 5;
    b = 10;
    c = 15;
    //do stuff
}
Constructor(int x, int y)
{
    a = x;
    b = y;
    c = 15;
    //do stuff
}
Constructor(int x, int y, int z)
{
    a = x;
    b = y;
    c = z;
    //do stuff
}

To prevent duplication of “stuff” and a few assignments, I tried out something like:

int a, b, c;

Constructor(): this(5, 10, 15)
{
}
Constructor(int x, int y): this(x, y, 15)
{
}
Constructor(int x, int y, int z)
{
    a = x;
    b = y;
    c = z;
    //do stuff
}

This works for what I want to do, but sometimes I need to use some lengthy code to create new objects or do some calculations:

int a, b, c;

Constructor(): this(new Something(new AnotherThing(param1, param2, param3),
    10, 15).CollectionOfStuff.Count, new SomethingElse("some string", "another
    string").GetValue(), (int)Math.Floor(533 / 39.384))
{
}
Constructor(int x, int y): this(x, y, (int)Math.Floor(533 / 39.384))
{
}
Constructor(int x, int y, int z)
{
    a = x;
    b = y;
    c = z;
    //do stuff
}

This code is pretty much the same as before, only the parameters that are being passed aren’t very readable. I would prefer doing something like:

int a, b, c;

Constructor(): this(x, y, z) //compile error, variables do not exist in context
{
    AnotherThing at = new AnotherThing(param1, param2, param3);
    Something st = new Something(aThing, 10, 15)
    SomethingElse ste = new SomethingElse("some string", "another string");

    int x = thing.CollectionOfStuff.Count;
    int y = ste.GetValue();
    int z = (int)Math.Floor(533 / 39.384);

    //In Java, I think you can call this(x, y, z) at this point.
    this(x, y, z); //compile error, method name expected
}
Constructor(int x, int y): this(x, y, z) //compile error
{
    int z = (int)Math.Floor(533 / 39.384);
}
Constructor(int x, int y, int z)
{
    a = x;
    b = y;
    c = z;
    //do stuff
}

Basically I’m building the parameters within the constructor body. Then I’m trying to pass those built parameters to another constructor. I think I remember being able to use the “this” and “super” keywords to call constructors while inside the body of another constructor when coding in Java. It doesn’t seem possible in C#.

Is there a way to do this easily? Did I do something incorrectly? If this is not possible, should I just stick with the unreadable code?

I guess I could always cut the duplicated code into another method completely outside the constructors. Then each constructor would just do its own thing and call the code shared by the other constructors.

  • 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-03T01:10:32+00:00Added an answer on June 3, 2026 at 1:10 am

    As an alternative to calling an initialization method from all constructors (which prevents you from using readonly fields) or factory methods (which introduce additional complexity when you have derived classes), you can use a parameter object:

    int a, b, c;
    
    public Constructor()
        : this(new ConstructorParameters())
    {
    }
    
    public Constructor(int x, int y)
        : this(new ConstructorParameters(x, y))
    {
    }
    
    public Constructor(int x, int y, int z)
    {
        a = x;
        b = y;
        c = z;
        //do stuff 
    } 
    
    private Constructor(ConstructorParameters parameters)
        : this(parameters.X, parameters.Y, parameters.Z)
    {
    }
    
    private class ConstructorParameters
    {
        public int X;
        public int Y;
        public int Z;
    
        public ConstructorParameters()
        {
            AnotherThing at = new AnotherThing(param1, param2, param3); 
            Something st = new Something(at, 10, 15) 
            SomethingElse ste = new SomethingElse("some string", "another string"); 
    
            X = st.CollectionOfStuff.Count; 
            Y = ste.GetValue(); 
            Z = (int)Math.Floor(533 / 39.384); 
        }
    
        public ConstructorParameters(int x, int y)
        {
            X = x;
            Y = y;
            Z = (int)Math.Floor(533 / 39.384);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I use default arguments in a constructor like this maybe Soldier(int entyID, int
So I've got a class like this: public class A { internal A(int i)
Like this: public class remoteStatusCounts : RemoteStatus { public int statusCount; public remoteStatusCounts(RemoteStatus r)
Ok, this is for homework about hashtables, but this is the simple stuff I
I am trying to compile this code: class OthelloState { public: // constructor Othello(int
I have a class like this: class A(arg: Int)(implicit i: Boolean) { def apply(v:
So I'm getting experience on this stuff little by little, but this problem seems
I have Constructor Tree(int a, int b, int c) and second Constructor Tree(int a,
I have the following code: Some functions: A::A(int i_a) {cout<<int Ctor\n;} //conversion constructor void
In .NET, there is a constructor for Dictionary<TKey, TValue> that takes one parameter, int

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.