C#.Net find output programs (Namespace) | set 2

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.
Submitted by Nidhi, on February 10, 2021

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);
    }
}

Output:

3
Press any key to continue . . .

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.

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);
    }
}

Output:

main.cs(9,4): error CS1525: Unexpected symbol `union'
main.cs(11,15): error CS1525: Unexpected symbol `int'
main.cs(13,14): error CS1514: Unexpected symbol `public', expecting `.' or `{'
main.cs(13,15): error CS1525: Unexpected symbol `A'

Explanation:

The above program will generate compile-time errors because we cannot create a union in the C#. C# does not support union.

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();
    }
}

Output:

Num: 10
Num: 20
Press any key to continue . . .

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.

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();
    }
}

Output:

main.cs(3,7): error CS1525: Unexpected symbol `namespace'

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.

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();
    }
}

Output:

main.cs(3,0): error CS1525: Unexpected symbol `const'

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.





Comments and Discussions!

Load comments ↻





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