Question

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ContructorChaining
{
    class AdditionOdMultipleNum
    {
        public AdditionOdMultipleNum(int a)
        {
            Console.WriteLine("sum of {0},1 is : {1}", a, (a + 1));

        }
        public AdditionOdMultipleNum(int a,int b)
            :this(a)
        {
            Console.WriteLine("sum of {0},{1} is : {2}", a, b, (a + b));
        }
        public AdditionOdMultipleNum(int a,int b,int c)
            :this(a ,b)
        {
            Console.WriteLine("sum of {0},{1},{2} is : {3}", a, b, c, (a + b + c));
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            AdditionOdMultipleNum addnum = new AdditionOdMultipleNum(2, 2, 6);

            Console.ReadKey(true);
        }
    }
}

In above example the output shown by the program, is 3,4,10 So, is it possible to see output in fashion 10,4,3

In other words can I change the order of execution of constructors i.e. Constructor with 3 parameters(with output) then it calls Constructor with 2 parameters(with output) finally it calls Constructor with 1 parameters(with output)

Was it helpful?

Solution 2

This sort of chaining is not directly possible; however you may be able to accomplish something similar with a factory method:

class AdditionOdMultipleNum
{
    private AdditionOdMultipleNum() {}
    // A private constructor means that only methods 
    // within this class can construct new object directly
    // so let's make some factory methods:

    public static AdditionOdMultipleNum Create(int a)
    {
        Console.WriteLine("sum of {0},1 is : {1}", a, (a + 1));
        AdditionOdMultipleNum instance = new AdditionOdMultipleNum();
        // you can modify the instance here
        return instance;

    }
    public static AdditionOdMultipleNum Create(int a,int b)
    {
        Console.WriteLine("sum of {0},{1} is : {2}", a, b, (a + b));
        AdditionOdMultipleNum instance = Create(a);
        // you can modify the instance here
        return instance;

    }
    public static AdditionOdMultipleNum Create(int a,int b,int c)
    {
        Console.WriteLine("sum of {0},{1},{2} is : {3}", a, b, c, (a + b + c));
        AdditionOdMultipleNum instance = Create(a, b);
        // you can modify the instance here
        return instance;
    }
}

Now you could call

 AdditionOdMultipleNum addnum = AdditionOdMultipleNum.Create(2, 2, 6);

This will result in the output in the desired order 10, 4, 3

OTHER TIPS

Offload all of the constructors to methods, thus allowing you to provide the exact semantics that you want by having the methods call each other whereever you want within the method body:

class AdditionOdMultipleNum
{
    public AdditionOdMultipleNum(int a)
    {
        Init(a);
    }
    public AdditionOdMultipleNum(int a, int b)
    {
        Init(a, b);
    }
    public AdditionOdMultipleNum(int a, int b, int c)
    {
        Init(a, b, c);
    }

    private void Init(int a)
    {
        Console.WriteLine("sum of {0},1 is : {1}", a, (a + 1));
    }
    private void Init(int a, int b)
    {
        Console.WriteLine("sum of {0},{1} is : {2}", a, b, (a + b));
        Init(a);
    }
    private void Init(int a, int b, int c)
    {
        Console.WriteLine("sum of {0},{1},{2} is : {3}", a, b, c, (a + b + c));
        Init(a, b);
    }
}

Not possible. When a constructor chains to another constructor, the body of that other constructor runs before the body of the first constructor. That can't be changed.

This seems like a simplified/contrived example though (you may have simplified it for posting so it's easy to understand, which is a good thing, thank you); maybe if you can describe your real scenario, someone might be able to suggest a tweak or design change to make it behave the way you want?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top