ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

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.
Submitted by Nidhi, on February 08, 2021

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

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#.

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

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.

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

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.

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

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().

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

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.

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.