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

Find the output of C#.Net programs | Structure | Set 1: 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
    {
        int A = 10;
        int 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(7,9): error CS0573: 'Demo.Sample': 
Structs cannot have instance property or field initializers
main.cs(8,9): error CS0573: 'Demo.Sample': 
Structs cannot have instance property or field initializers

Explanation:

The above program will generate because by default members of a structure are private in C# that's why they cannot be accessed outside the class.

Question 2:

using System;

namespace Demo
{
    struct Sample
    {
        public int A = 10;
        public int 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(7,16): error CS0573: 'Demo.Sample': 
Structs cannot have instance property or field initializers
main.cs(8,16): error CS0573: 'Demo.Sample': 
Structs cannot have instance property or field initializers

Explanation:

The above program will generate syntax error because, in the above program, we initialized members of the structure at the time of declaration, which is not correct, if we want to initialize members of structure we need to create a method to initialize them.

Question 3:

using System;

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

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

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

            Console.WriteLine(C);
        }
    }
}

Output:

30
Press any key to continue . . .

Explanation:

In the above program, we created a structure that contains two public data members A and B, and we create a method init() with internal access specifiers to initialize the value of data members and A and B by 10 and 20 respectively.

Let's come to the Main() method, here we created S reference for structure Sample and declared a local variable C after that sum of A and B assigned to the C and print C, that is 30 printed on the console screen.

Question 4:

using System;

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

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

            S.A = 10;
            S.B = 20;

            C = S.A + S.B;

            Console.WriteLine(C);
        }
    }
}

Output:

30
Press any key to continue . . .

Explanation:

In the above program, we created a structure that contains two public data members A and B.

Let's come to the Main() method, here we created S variable for structure Sample, here we did not use new operator, as we know that the structure is a value type that's why it is not necessary to use new operator to create a variable of a structure, and we declared a local variable C, after that sum of A and B assigned to the C and print C, that is 30 printed on the console screen.

Question 5:

using System;

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

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

            C = S.A + S.B;

            Console.WriteLine(C);
        }
    }
}

Output:

main.cs(19,19): error CS0170: Use of possibly unassigned field `A'
main.cs(19,25): error CS0170: Use of possibly unassigned field `B'

Explanation:

The above program will generate syntax error because we cannot use uninitialized data members of a structure.





Comments and Discussions!

Load comments ↻





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