2
Answers

NP46 constructor & non-constructor

Ask a question
Maha

Maha

17y
2k
1

Hi Guys

 

NP46   constructor & non-constructor

 

Though output is same in the following programs one is using constructor method other one is not.

 

I wish to know in what circumstances we have to decide which method is suitable. Anyone knows please explain.

         

Thank you

 

using System;

public class CreateEmployee

{

    public static void Main()

    {

        Employee myAssistant = new Employee();

 

        myAssistant.IDNumber = 345;

 

        Console.WriteLine("ID # is {0}", myAssistant.IDNumber);

    }

}

 

class Employee

{

    private int idNumber;

 

    public int IDNumber            

    {

        get{ return idNumber; }

        set{ idNumber = value; }

    }

}

//ID # is 345

 

using System;

public class CreateEmployee

{

    public static void Main()

    {

        Employee myAssistant = new Employee(345);

 

        Console.WriteLine("ID # is {0}", myAssistant.IDNumber);

    }

}

 

class Employee

{

    private int idNumber;

 

    public Employee(int i)               

    {

         idNumber = i;

    }

    public int IDNumber            

    {

        get { return idNumber; }

    }

}

//ID # is 345


Answers (2)