Find the output of C#.Net programs | Classes & Objects | Set 3: Enhance the knowledge of C#.Net Classes & Objects 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;
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee emp = new Employee();
Console.WriteLine(sizeof(emp));
}
}
}
Question 2:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee emp = new Employee();
Console.WriteLine(emp);
}
}
}
Question 3:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee emp = new Employee();
Console.WriteLine(emp.GetType());
}
}
}
Question 4:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee emp = new Employee();
if (emp == (Employee)emp.GetType())
Console.WriteLine("Hello");
else
Console.WriteLine("Hiii");
}
}
}
Question 5:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee emp = new Employee();
if (emp.ToString() == emp.GetType().ToString())
Console.WriteLine("Hello");
else
Console.WriteLine("Hiii");
}
}
}
Answer 1:
Output:
Explanation:
The above program will generate syntax error because we cannot get the size of an object using the sizeof() operator in C#.
Answer 2:
Output:
Explanation:
In the above program, we created a class Employee that contains three data members emp_id, name, and salary.
Now look to the Main() method - Here, we created an object of Employee class,
Console.WriteLine(emp);
The above statement will print the name of the class with namespace on the console screen.
Answer 3:
Output:
Explanation:
In the above program, we created a class Employee that contains three data members emp_id, name, and salary.
Now look to the Main() method - Here, we created an object of Employee class,
Console.WriteLine(emp.GetType());
In the above statement, the GetType() method will print the name of the class with namespace on the console screen.
Answer 4:
Output:
Explanation:
The above program will generate syntax error, GetType() method return System.Type that cannot be converted into Employee class, that's why we cannot compare object emp with returned value of emp.GetType().
Answer 5:
Output:
Explanation:
The above program will print "Hello" on the console screen. In the above program, we created a class Employee that contains three data members emp_id, name, and salary.
Now look to the Main() method - Here, we created an object of Employee class.
The emp.ToString() and emp.GetType().ToString(), both will return Demo.Employee that's why "Hello" will print on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer