Skip to main content
added 517 characters in body
Source Link
Oguz Ozgul
  • 853
  • 5
  • 9

You can invoke constructor overloads from each other as such:

public Prix(double valeur, string monnaie)

calls

public Prix(double valeur)

by:

public Prix(double valeur, string monnaie) : this(valeur)

Therefore,

You can simplify the exact same behaviour at least in terms of the final state as follows:

class Prix
{
    double valeur;
    string monnaie = "€";

    public Prix(double valeur, string monnaie)
        : this(valeur)
    {
        if (monnaie.Equals("€") || monnaie.Equals("$"))
        {
            this.monnaie = monnaie;
        }
    }

    public Prix(double valeur)
    {
        this.valeur = valeur;
    }
}

EDIT 1:

You also don't have to check and assign moannie if it is passed in as euro because it already has the value euro:

class Prix
{
    double valeur;
    string monnaie = "€";

    public Prix(double valeur, string monnaie)
        : this(valeur)
    {
        if (monnaie.Equals("$"))
        {
            this.monnaie = monnaie;
        }
    }

    public Prix(double valeur)
    {
        this.valeur = valeur;
    }
}

You can invoke constructor overloads from each other as such:

public Prix(double valeur, string monnaie)

calls

public Prix(double valeur)

by:

public Prix(double valeur, string monnaie) : this(valeur)

Therefore,

You can simplify the exact same behaviour at least in terms of the final state as follows:

class Prix
{
    double valeur;
    string monnaie = "€";

    public Prix(double valeur, string monnaie)
        : this(valeur)
    {
        if (monnaie.Equals("€") || monnaie.Equals("$"))
        {
            this.monnaie = monnaie;
        }
    }

    public Prix(double valeur)
    {
        this.valeur = valeur;
    }
}

You can invoke constructor overloads from each other as such:

public Prix(double valeur, string monnaie)

calls

public Prix(double valeur)

by:

public Prix(double valeur, string monnaie) : this(valeur)

Therefore,

You can simplify the exact same behaviour at least in terms of the final state as follows:

class Prix
{
    double valeur;
    string monnaie = "€";

    public Prix(double valeur, string monnaie)
        : this(valeur)
    {
        if (monnaie.Equals("€") || monnaie.Equals("$"))
        {
            this.monnaie = monnaie;
        }
    }

    public Prix(double valeur)
    {
        this.valeur = valeur;
    }
}

EDIT 1:

You also don't have to check and assign moannie if it is passed in as euro because it already has the value euro:

class Prix
{
    double valeur;
    string monnaie = "€";

    public Prix(double valeur, string monnaie)
        : this(valeur)
    {
        if (monnaie.Equals("$"))
        {
            this.monnaie = monnaie;
        }
    }

    public Prix(double valeur)
    {
        this.valeur = valeur;
    }
}
Source Link
Oguz Ozgul
  • 853
  • 5
  • 9

You can invoke constructor overloads from each other as such:

public Prix(double valeur, string monnaie)

calls

public Prix(double valeur)

by:

public Prix(double valeur, string monnaie) : this(valeur)

Therefore,

You can simplify the exact same behaviour at least in terms of the final state as follows:

class Prix
{
    double valeur;
    string monnaie = "€";

    public Prix(double valeur, string monnaie)
        : this(valeur)
    {
        if (monnaie.Equals("€") || monnaie.Equals("$"))
        {
            this.monnaie = monnaie;
        }
    }

    public Prix(double valeur)
    {
        this.valeur = valeur;
    }
}