Home »
C#.Net »
C#.Net find output programs
C#.Net find output programs (Namespace) | set 1
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.
Submitted by Nidhi, on February 10, 2021
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);
}
}
Output:
C : 30
Press any key to continue . . .
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.
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);
}
}
Output:
main.cs(19,9): error CS0432: Alias `MyNameSpace' not found
main.cs(20,9): error CS0432: Alias `MyNameSpace' not found
main.cs(22,13): error CS0432: Alias `MyNameSpace' not found
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#.
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);
}
}
Output:
C : 30
Press any key to continue . . .
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.
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);
}
}
Output:
main.cs(11,11): error CS1525: Unexpected symbol `void', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
main.cs(13,8): error CS1525: Unexpected symbol `Console', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
main.cs(13,25): error CS1525: Unexpected symbol `(', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
main.cs(14,5): error CS1514: Unexpected symbol `}', expecting `.' or `{'
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.
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);
}
}
Output:
main.cs(24,25): error CS0122: `MyNameSpace.Str.Num' is inaccessible due to its protection level
main.cs(26,49): error CS0122: `MyNameSpace.Str.Num' is inaccessible due to its protection level
Explanation:
The above program will generate syntax errors because the structure STR contains private member Num that cannot be accessed outside the structure directly.