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

Find the output of C#.Net programs | Structure | Set 2: Enhance the knowledge of C#.Net Structure concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 07, 2021

Question 1:

using System;

namespace Demo
{
    struct Sample
    {
        public int A;
        public int B;

        public void init()
        {
            A = 10;
            B = 20;
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int C = 0;
            Sample S;

            S.init();
            C = S.A + S.B;

            Console.WriteLine(C);
        }
    }
}

Output:

main.cs(25,13): error CS0165: Use of unassigned local variable `S'

Explanation:

The above program will generate syntax error, because if a structure contains a method then we need to use a new operator to create a reference of structure.

Question 2:

using System;

namespace Demo
{
    struct Sample
    {
        public int A;
        public int B;

        public Sample()
        {
            A = 10;
            B = 20;
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int C = 0;
            Sample S= new Sample();

            C = S.A + S.B;

            Console.WriteLine(C);
        }
    }
}

Output:

main.cs(10,16): error CS0568: 
Structs cannot contain explicit parameterless constructors

Explanation:

The above program will generate syntax error because a structure cannot contain explicit default or no-argument constructor in C#.

Question 3:

using System;

namespace Demo
{
    struct Sample
    {
        int A;
        int B;

        public Sample(int a, int b)
        {
            A = a;
            B = b;
        }

        public int Sum()
        {
            return A + B;
        }

    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int C = 0;
            Sample S= new Sample(10,20);

            C = S.Sum();

            Console.WriteLine(C);
        }
    }
}

Output:

30
Press any key to continue . . .

Explanation:

In the above program, we created a structure that contains two data members A and B and we also created a parameterized constructor and a method Sum().

The parameterized constructor is used to initialize data members A and B, and method Sum() is used to return the addition of both A and B.

In the Main() method, I created a reference of structure and initialized using the parameterized constructor.

Then call method Sum that will return the addition of 10 and 20, that is 30, assigned to local variable C and printed on the console screen.

Question 4:

using System;

namespace Demo
{
    struct Sample
    {
        int A;
        int B;

        public Sample(int A, int B)
        {
           this.A=A;
           this.B=B;
        }

        public int Sum()
        {
            return A + B;
        }

    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int C = 0;
            Sample S= new Sample(10,20);

            C = S.Sum();

            Console.WriteLine(C);
        }
    }
}

Output:

30
Press any key to continue . . .

Explanation:

In the above program, we created a structure that contains two data members A and B and we also created a parameterized constructor and a method Sum().

The parameterized constructor is used to initialize data member A and B, In the parameterized constructor A and B are data member and parameters, then we used this object to differentiate data members from local arguments, and the method Sum() is used to return the addition of both A and B.

In the Main() method, I created a reference of structure and initialized using the parameterized constructor.

Then call method Sum that will return the addition of 10 and 20, that is 30, assigned to local variable C and printed on the console screen.

Question 5:

using System;

namespace Demo
{
    struct Sample
    {
        int A;
        int B;

        public Sample(int A, int B)
        {
           this.A=A;
           this.B=B;
        }

        public int Sum()
        {
            return A + B;
        }

        public ~Sample()
        {
            Console.WriteLine("Destructor called");
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int C = 0;
            Sample S= new Sample(10,20);

            C = S.Sum();

            Console.WriteLine(C);
        }
    }
}

Output:

main.cs(21,17): error CS0575: Only class types can contain destructor
main.cs(21,17): error CS0106: The modifier `public' is not valid for this item

Explanation:

The above program will generate syntax error because a structure cannot contain a destructor.





Comments and Discussions!

Load comments ↻





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