Link to home
Start Free TrialLog in
Avatar of nicedone
nicedone

asked on

i want to understand how c# class with 3 constructers with mismatching base parameters work

Hi,

In one of the projects that i work  at i have come accross a class like below,

could yo help me how it works ? because i did not fully got how base can have 2 parameters but the constructer takes 3 and i did not fully understand the first 3 constructers using this how it works?


public class CustomHttpException : Exception
    {
        public CustomHttpException(string message) : this(message, HttpStatusCode.InternalServerError, null) { }
        public CustomHttpException(string message, Exception innerException) : this(message, HttpStatusCode.InternalServerError, innerException) { }
        public CustomHttpException(string message, HttpStatusCode statusCode) : this(message, statusCode, null) { }
        
        public CustomHttpException(string message, HttpStatusCode statusCode, Exception innerException)
            : base(message, innerException)
        {
            StatusCode = statusCode;
        }

        public HttpStatusCode StatusCode { get; set; }
    }

Open in new window

SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this is for compilation of the code that is calling the constructor.

for this line:
CustomHttpException x = new CustomHttpException ("some string");

only the first constructor can be taken, and it matches with the data types.

with this line of code, the compiler will check what type of object "some_exception" is:
CustomHttpException x = new CustomHttpException ("some string", some_exception);

if it is of type Exception, then the 2nd constructor is used
if it is of type HttpStatusCode , then the 3rd constructor is used
if its neither of the 2 types, a compile error will be returned

the last contructor is bascially just having 1 more argument, and using one of the of the other constructors + some function code to do the rest (what the other constructor is not doing)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial