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.
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Answer 1:
Output:
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.
Answer 2:
Output:
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.
Answer 3:
Output:
Explanation:
The above program will generate syntax error because Integer is not any type or object in C#.
Answer 4:
Output:
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.
Answer 5:
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer