Home »
C#.Net »
C#.Net find output programs
C#.Net find output programs (Constructors & Destructors) | set 1
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.
Submitted by Nidhi, on February 08, 2021
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();
}
}
}
Output:
main.cs(31,28): error CS0122: `Demo.Employee.Employee()' is inaccessible due to its protection level
main.cs(11,9): (Location of the symbol related to previous error)
main.cs(33,17): error CS0122: `Demo.Employee.Print()' is inaccessible due to its protection level
main.cs(18,14): (Location of the symbol related to previous error)
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.
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();
}
}
}
Output:
Emp ID: 101
Name : Harry
Salary: 10000
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 object emp of Employee class and initialize members and print employee detail using the Print() method.
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();
}
}
}
Output:
main.cs(31,29): error CS1729: The type `Demo.Employee' does not contain a constructor that takes `0' arguments
main.cs(11,16): (Location of the symbol related to previous error)
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.
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();
}
}
}
Output:
Emp ID: 100
Name : John
Salary: 20000
Emp ID: 101
Name : Harry
Salary: 10000
Press any key to continue . . .
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.
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();
}
}
}
Output:
main.cs(11,26): error CS1525: Unexpected symbol `emp_id', expecting `base' or `this'
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++.