Q:

C#.Net find output programs (Classes & Objects) | set 3

belongs to collection: C#.Net find output programs

0

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");
        }
    }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Output:

main.cs(19,38): error CS0246: The type or namespace name `emp' could not be found. 
Are you missing an assembly reference?

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:

Demo.Employee
Press any key to continue . . .

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:

Demo.Employee
Press any key to continue . . .

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:

main.cs(19,38): error CS0030: Cannot convert type `System.Type' to `Demo.Employee'

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:

Hello
Press any key to continue . . .

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

total answers (1)

C#.Net find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C#.Net find output programs (Constructors & Destru... >>
<< C#.Net find output programs (Classes & Objects) | ...