0
Find the output of C#.Net programs | Constructors & Destructors | Set 1: Enhance the knowledge of C#.Net Constructors & Destructors concepts by solving and finding the output of some C#.Net programs.
Question 1:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
Employee()
{
emp_id = 101;
name = "Dummy";
salary = 10000;
}
void Print()
{
Console.WriteLine("Emp ID: " + emp_id);
Console.WriteLine("Name : " + name );
Console.WriteLine("Salary: " + salary);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee emp = new Employee();
emp.Print();
}
}
}
Question 2:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
public Employee(int id, string na, int sal)
{
emp_id = id;
name = na;
salary = sal;
}
public void Print()
{
Console.WriteLine("Emp ID: " + emp_id);
Console.WriteLine("Name : " + name );
Console.WriteLine("Salary: " + salary);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee emp = new Employee(101,"Harry",10000);
emp.Print();
}
}
}
Question 3:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
public Employee(int id, string na, int sal)
{
emp_id = id;
name = na;
salary = sal;
}
public void Print()
{
Console.WriteLine("Emp ID: " + emp_id);
Console.WriteLine("Name : " + name );
Console.WriteLine("Salary: " + salary);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee emp1 = new Employee();
Employee emp2 = new Employee(101,"Harry",10000);
emp1.Print();
emp2.Print();
}
}
}
Question 4:
using System;
namespace Demo
{
class Employee
{
int emp_id=100;
string name="John";
int salary=20000;
public Employee()
{ }
public Employee(int id, string na, int sal)
{
emp_id = id;
name = na;
salary = sal;
}
public void Print()
{
Console.WriteLine("Emp ID: " + emp_id);
Console.WriteLine("Name : " + name );
Console.WriteLine("Salary: " + salary);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee emp1 = new Employee();
Employee emp2 = new Employee(101,"Harry",10000);
emp1.Print();
emp2.Print();
}
}
}
Question 5:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
public Employee():emp_id(10),name("John"),salary(20000)
{ }
public Employee(int id, string na, int sal)
{
emp_id = id;
name = na;
salary = sal;
}
public void Print()
{
Console.WriteLine("Emp ID: " + emp_id);
Console.WriteLine("Name : " + name );
Console.WriteLine("Salary: " + salary);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee emp1 = new Employee();
Employee emp2 = new Employee(101,"Harry",10000);
emp1.Print();
emp2.Print();
}
}
}
Answer 1:
Output:
Explanation:
The above program will generate syntax errors because we defined private constructor and Print() method is also private in Employee class, and we know that we cannot access a private member of class outside the class.
Answer 2:
Output:
Explanation:
In the above program, we created a class Sample that contains data member emp_id, name, and salary. Here, we defined a parameterized constructor to initialize the data members, and we also defined a Print() method to print the values of data members.
Now look to the Program class, the Program class contains the Main() method, which is the entry program of the program. Here, we created an object emp of Employee class and initialize members and print employee detail using the Print() method.
Answer 3:
Output:
Explanation:
The above program will generate syntax error, because of the below statement,
In the above statement, we created an object of Employee class using default constructor, but we did not define any default or zero-argument constructor inside the employee class.
Answer 4:
Output:
Explanation:
In the above program, we created a class Sample that contains data member emp_id, name, and salary initialized with 100, "john", and 2000 respectively. Here, we defined a default constructor with an empty body and a parameterized constructor to initialize the data members with specified values, and we also defined a Print() method to print the values of data members.
Now look to the Program class, the Program class contains the Main() method, which is the entry program of the program. Here, we created two objects emp1 and emp2.
Here, object emp1 calls default constructor, but default constructor is defined with the empty body that's why data members of the object contain the pre-initialized values that are printed by the Print() method.
Here, object emp2 calls parameterized constructor, and initialized by specified values and print them using the Print() method on the console screen.
Answer 5:
Output:
Explanation:
The above program will generate syntax errors because of the below code,
public Employee():emp_id(10),name("John"),salary(20000) { }
This type of member initialization is not supported in C#. It looks like a member initializer list used in C++.
need an explanation for this answer? contact us directly to get an explanation for this answer