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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:48:56+00:00 2026-05-30T07:48:56+00:00

Suppose for instance I’m defining a Complex class for representing complex numbers. I would

  • 0

Suppose for instance I’m defining a Complex class for representing complex numbers. I would like to define two constructors, so that I can write for example:

Complex z1 = new Complex(x: 4, y: 3);
Complex z2 = new Complex(r: 2, theta: Math.PI / 4);

However, I cannot define the constructors like this:

public Complex(double x, double y) { ... }
public Complex(double r, double theta) { ... }

because both constructors would have the same signature, which is not allowed. But in C# 4 I can write this, using an optional argument:

public Complex(double x, double y) { ... }
public Complex(double r, double theta, bool unused=true) { ... }

It works, I can then use the above constructor calls as intended. The sole purpose of the unused argument is to make the signatures different; it’s totally unused, both when defining and when calling the constructor.

To me this seems to be a an ugly trick: is there any better option?

  • 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-30T07:48:58+00:00Added an answer on May 30, 2026 at 7:48 am

    Make the constructor private and have a static factory style function.

    public static Complex CreateComplexPolar(double r, double theta);
    public static Complex CreateComplex(double x, double y);
    

    You can do validation on the inputs based on what they should be.

    Another possibility would be to create a type that encapsulates the inputs and use constructors as you previously mentioned.

    public struct PolarCoordinates
    {
      public double Rho;
      public double Theta;
    }
    
    public struct CartesianCoordinates
    {
      public double X;
      public double Y;
    }
    
    public Complex(PolarCoordinates pc);
    public Complex(CartesianCoordinates cc);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that I have a class like this class Employee: pass I create two
Suppose I have a Singleton class (any class can get the instance): class data
Let spam be an instance of some class Spam , and suppose that spam.ham
Suppose I have a class with a string instance attribute. Should I initialize this
In C#, suppose you have an object (say, myObject ) that is an instance
Suppose I have a instance variable that has original value: Integer mMyInt = 1;
Suppose I have an anonymous class instance var foo = new { A =
For instance, I suppose I have a directory that contains the following folders foo_bar
Suppose I have a very small float a (for instance a=0.5 ) that enters
Suppose I have a Monkey class which sometimes needs to acquire an instance of

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.