Home »
C#.Net »
C#.Net find output programs
C#.Net find output programs (Constructors & Destructors) | set 3
Find the output of C#.Net programs | Constructors & Destructors | Set 3: Enhance the knowledge of C#.Net Constructors & Destructors concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 08, 2021
Question 1:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
public Employee()
{
emp_id = 100;
name = "john";
salary = 1000;
}
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+"\n");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee[] emp = { new Employee(), new Employee(101, "Potter", 2000) };
emp[0].Print();
emp[1].Print();
}
}
}
Output:
Emp ID: 100
Name : John
Salary: 1000
Emp ID: 101
Name : Potter
Salary: 2000
Press any key to continue . . .
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 array of objects and initialized using default and parameterized constructor and print values of data members using the Print() method.
Question 2:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
public Employee()
{
emp_id = 100;
name = "john";
salary = 1000;
}
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+"\n");
}
public ~Employee()
{
Console.WriteLine("Destructor called");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee E1 = new Employee(101, "Herry", 1000);
Employee E2 = new Employee(101, "Potter", 2000);
E1.Print();
E2.Print();
}
}
}
Output:
main.cs(32,17): error CS0106: The modifier `public' is not valid for this item
Explanation:
The above program will generate syntax error because the public access modifier is not valid for destructors in C#.
Question 3:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
public Employee()
{
emp_id = 100;
name = "john";
salary = 1000;
}
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+"\n");
}
~Employee()
{
Console.WriteLine("Destructor called");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee E1 = new Employee(101, "Herry", 1000);
Employee E2 = new Employee(101, "Potter", 2000);
E1.Print();
E2.Print();
}
}
}
Output:
Emp ID: 101
Name : Herry
Salary: 1000
Emp ID: 101
Name : Potter
Salary: 2000
Destructor called
Destructor called
Press any key to continue . . .
Explanation:
In the above program, we created a class Sample that contains data member emp_id, name, and salary. Here, we defined default and parameterized constructor to initialize the data members, and we also defined a Print() method to print the values of data members, and we defined a destructor that will call automatically we scope of the object get finished.
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 of E1 and E2. Then we call the Print() method with E1 and E2. After that destructor called twice when the scope of E1 and E2 get finished.
Question 4:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
public Employee()
{
emp_id = 100;
name = "john";
salary = 1000;
}
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+"\n");
}
~Employee()
{
Console.WriteLine("Destructor called");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee E1 = new Employee(101, "Herry", 1000);
Employee E2 = new Employee(101, "Potter", 2000);
E1.Print();
E2.Print();
delete E1;
delete E2;
}
}
}
Output:
main.cs(49,20): error CS0128: A local variable named `E1' is already defined in this scope
main.cs(50,20): error CS0128: A local variable named `E2' is already defined in this scope
Explanation:
The above program will generate errors because delete is not a built-in keyword in C#. That's why the below statements will generate errors,
delete E1;
delete E2;
Question 5:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
public Employee()
{
emp_id = 100;
name = "john";
salary = 1000;
}
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+"\n");
}
~Employee()
{
Console.WriteLine("Destructor called");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee[] E = { new Employee(101, "Herry", 1000), new Employee(101, "Potter", 2000) };
E[0].Print();
E[1].Print();
}
}
}
Output:
Emp ID: 101
Name : Herry
Salary: 1000
Emp ID: 101
Name : Potter
Salary: 2000
Destructor called
Destructor called
Press any key to continue . . .
Explanation:
In the above program, we created a class Sample that contains data member emp_id, name, and salary. Here, we defined default and parameterized constructor to initialize the data members, and we also defined a Print() method to print the values of data members, and we defined a destructor that will call automatically we scope of the object get finished.
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 array of objects. Then we call Print() method with E[0] and E[1]. After that destructor called twice when the scope of E[0] and E[1] get finished.