C#.Net find output programs (Boxing & Unboxing) | set 1

Find the output of C#.Net programs | Boxing & Unboxing | Set 1: Enhance the knowledge of C#.Net Boxing & Unboxing concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 07, 2021

Question 1:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        int num = 10;

        object ob = num;

        num = 20;

        Console.WriteLine(num);
        Console.WriteLine(ob);
    }
}

Output:

20
10
Press any key to continue . . .

Explanation:

In the above program, we created a local variable num initialized with 10.

object ob = num;

In the above statement, we perform boxing of variable num and assigned to object ob, and then we assigned the value 20 into variable num, and then print the value of num and ob on the console screen.

Question 2:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        int A = 10;
        int C = 0;

        object B =A;

        A = 20;

        C = A + B;

        Console.WriteLine(C);
    }
}

Output:

main.cs(15,13): error CS0019: Operator `+' cannot be applied to 
operands of type `int' and `object'

Explanation:

The above program will generate a compile-time error because here B an object and A an integer variable that cannot be added directly, we need to typecast the value of object B.

Question 3:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        int A = 10;
        int C = 0;

        Integer B =A;

        A = 20;

        C = A + (int)B;

        Console.WriteLine(C);
    }
}

Output:

main.cs(11,9): error CS0246: The type or namespace name `Integer' 
could not be found. Are you missing an assembly reference?
main.cs(15,22): error CS0841: A local variable `B' 
cannot be used before it is declared

Explanation:

The above program will generate syntax error because Integer is not any type or object in C#.

Question 4:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        int A = 10;
        int C = 0;

        Object B =A;

        A = 20;

        C = A + (int)B;

        Console.WriteLine(C);
    }
}

Output:

30
Press any key to continue . . .

Explanation:

In the above program, we created two local variables A and C initialized with 10 and 0 respectively.

Object B =A;

In the above statement, we box the value of A into object B.

C = A + (int)B;

In the above statement, we un-boxed the value of object B and added to the variable A and assigned to the variable C. Then finally print the value of variable C on the console screen.

Question 5:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        const int A = 10;
        int C = 0;

        object B =A;

        B = 50;

        C = A + (int)B;

        Console.WriteLine(C);
    }
}

Output:

60
Press any key to continue . . .

Explanation:

In the above program, we created a constant A and variable C initialized with 10 and 0 respectively.

object B =A;

In the above statement, we boxed the value of A into object B.

C = A + (int)B;

In the above statement, we un-boxed the value of object B and added to the variable A and assigned to the variable C. Then finally print the value of variable C on the console screen.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.