Q:

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

belongs to collection: C#.Net find output programs

0

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.

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

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

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

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

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

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.

Answer 2:

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.

Answer 3:

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.

Answer 4:

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.

Answer 5:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C#.Net find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C#.Net find output programs (Structure) | set 2... >>
<< C#.Net find output programs (Boxing & Unboxing) | ...