Find the output of C#.Net programs | Namespace | Set 2: Enhance the knowledge of C#.Net Namespace concepts by solving and finding the output of some C#.Net programs.
Question 1:
using System;
namespace MyNameSpace
{
enum Vals
{
Val1 = 1, Val2, Val3, Val4
}
class A
{
public int Num;
public A()
{
Num = (int)Vals.Val3;
}
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
MyNameSpace.A ob = new MyNameSpace.A();
Console.WriteLine(ob.Num);
}
}
Question 2:
using System;
namespace MyNameSpace
{
enum Vals
{
Val1 = 1, Val2, Val3, Val4
}
union A
{
public int Num;
public A()
{
Num = (int)Vals.Val3;
}
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
MyNameSpace.A ob = new MyNameSpace.A();
Console.WriteLine(ob.Num);
}
}
Question 3:
using System;
namespace MyNameSpace
{
class A
{
int Num;
public A(int N)
{
Num= N;
}
public void Print()
{
Console.WriteLine("Num: "+Num);
}
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
MyNameSpace.A[] ob = new MyNameSpace.A[2] { new MyNameSpace.A(10), new MyNameSpace.A(20) };
ob[0].Print();
ob[1].Print();
}
}
Question 4:
using System;
public namespace MyNameSpace
{
class A
{
int Num;
public A(int N)
{
Num= N;
}
public void Print()
{
Console.WriteLine("Num: "+Num);
}
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
MyNameSpace.A[] ob = new MyNameSpace.A[2] { new MyNameSpace.A(10), new MyNameSpace.A(20) };
ob[0].Print();
ob[1].Print();
}
}
Question 5:
using System;
const namespace MyNameSpace
{
class A
{
int Num;
public A(int N)
{
Num= N;
}
public void Print()
{
Console.WriteLine("Num: "+Num);
}
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
MyNameSpace.A[] ob = new MyNameSpace.A[2] { new MyNameSpace.A(10), new MyNameSpace.A(20) };
ob[0].Print();
ob[1].Print();
}
}
Answer 1:
Output:
Explanation:
In the above program, we created a namespace MyNamespace that contains an enumeration Vals and a class A.
Here, enumeration Vals contains 4 constants Val1, Val2, Val3, and Val4 initialized 1, 2, 3, and 4 respectively.
Class A contains data member Num which is initialized by the Val3 enumeration constant in the constructor.
In the Main() method, we created object ob and print the value of variable Num on the console screen.
Answer 2:
Output:
Explanation:
The above program will generate compile-time errors because we cannot create a union in the C#. C# does not support union.
Answer 3:
Output:
Explanation:
In the above program, we created a namespace MyNameSpace that contains data member Num, constructor, and Print() method.
Now look to the Main() method,
MyNameSpace.A[] ob = new MyNameSpace.A[2] { new MyNameSpace.A(10), new MyNameSpace.A(20) }; ob[0].Print(); ob[1].Print();
Here, we created an array of objects of class A and initialized data members using parameterized constructor and then printed the values of data members on the console screen.
Answer 4:
Output:
Explanation:
The above program will generate syntax error because we cannot use any modifier or attribute in the namespace declaration, here we used the public modifier in the declaration of namespace MyNameSpace.
Answer 5:
Output:
Explanation:
In the above program, we used the const keyword in the declaration of namespace MyNameSpace. const keyword cannot be used in the namespace declaration.
need an explanation for this answer? contact us directly to get an explanation for this answer