Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a constructor include logic that determines which other constructor overrides to call?

It is posible in C# to decide in constructor, which other override constructor use? This below code doesn't compile! I don't know which invocation use.

    public IntRange(int val, bool isMax)
        : isMax ? this() : this()
    {
        if (isMax)
        {
            IntRange(0, val);
        }
        else
        {
            IntRange(val, int.MaxValue);
        }
    }
like image 688
Jacek Avatar asked Jun 14 '13 14:06

Jacek


1 Answers

How about:

    class IntRange {
      public IntRange(int val, bool isMax)
        : this(isMax ? 0 : val, isMax ? val : int.MaxValue) {
      }
      public IntRange(int min, int max) {
      }
    }
like image 180
Ondrej Svejdar Avatar answered Sep 25 '22 20:09

Ondrej Svejdar