Find the output of C#.Net programs | Namespace | Set 1: 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
{
class A
{
public static int Num1;
public static int Num2;
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
int C = 0;
MyNameSpace.A.Num1 = 10;
MyNameSpace.A.Num2 = 20;
C = MyNameSpace.A.Num1 + MyNameSpace.A.Num2;
Console.WriteLine("C : {0}", C);
}
}
Question 2:
using System;
namespace MyNameSpace
{
class A
{
public static int Num1;
public static int Num2;
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
int C = 0;
MyNameSpace::A.Num1 = 10;
MyNameSpace::A.Num2 = 20;
C = MyNameSpace::A.Num1 + MyNameSpace::A.Num2;
Console.WriteLine("C : {0}", C);
}
}
Question 3:
using System;
namespace MyNameSpace
{
class A
{
public static int Num;
}
namespace Nested
{
class A
{
public static int Num;
}
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
int C = 0;
MyNameSpace.A.Num = 10;
MyNameSpace.Nested.A.Num = 20;
C = MyNameSpace.A.Num + MyNameSpace.Nested.A.Num;
Console.WriteLine("C : {0}", C);
}
}
Question 4:
using System;
namespace MyNameSpace
{
class A
{
public static int Num1;
public static int Num2;
}
public void Fun()
{
Console.WriteLine("Hello Word");
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
int C = 0;
MyNameSpace.A.Num1 = 10;
MyNameSpace.A.Num2 = 20;
C = MyNameSpace.A.Num1 + MyNameSpace.A.Num2;
MyNameSpace.Fun();
Console.WriteLine("C : {0}", C);
}
}
Question 5:
using System;
namespace MyNameSpace
{
class A
{
public static int Num;
}
struct Str
{
static int Num;
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
int C = 0;
MyNameSpace.A.Num = 10;
MyNameSpace.Str.Num = 20;
C = MyNameSpace.A.Num + MyNameSpace.Str.Num;
Console.WriteLine("C : {0}", C);
}
}
Answer 1:
Output:
Explanation:
In the above program, we created a namespace MyNameSpace that contains a class A, and class A contains two public integer variables Num1 and Num2.
Now look to the Program class that contains the Main() method. Here, we created a local variable C and initialized Num1 and Num2 with 10 and 20 respectively. Then add values of Num1 and Num2 and assigned to variable C and then print the value of C on the screen.
Answer 2:
Output:
Explanation:
The above program will generate syntax errors because, in the above program, we used "::" operator to access members of the namespace, which is not correct, we need to use dot '.' operator to access members of the namespace in C#.
Answer 3:
Output:
Explanation:
In the above program, we created a namespace MyNameSpace that contains a class A, and a namespace nested. class A contains a public integer variable Num in both namespaces MyNameSpace and Nested.
Now look to the Program class that contains the Main() method. Here, we created a local variable C.
MyNameSpace.A.Num = 10; MyNameSpace.Nested.A.Num = 20;
Here, we initialized values of class members that contained in the namespaces,
C = MyNameSpace.A.Num + MyNameSpace.Nested.A.Num;
Add values of data members and assigned to local variable C, and printed the value of C on the console screen.
Answer 4:
Output:
Explanation:
The above program will generate a compile-time error because we cannot contain a method in the namespace directly, here namespace MyNameSpace contains Fun() method, which is not correct.
Answer 5:
Output:
Explanation:
The above program will generate syntax errors because the structure STR contains private member Num that cannot be accessed outside the structure directly.
need an explanation for this answer? contact us directly to get an explanation for this answer