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

Find the output of C#.Net programs | Structure | Set 3: 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;
        int B;

        public static 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:

main.cs(10,23): error CS0132: `Demo.Sample.Sample(int, int)': 
The static constructor must be parameterless
main.cs(10,23): error CS0515: `Demo.Sample.Sample(int, int)': 
static constructor cannot have an access modifier

Explanation:

The above program will generate syntax errors because we defined a static constructor inside the structure, but a static constructor must be parameter less and without any access modifier.

Question 2:

using System;

namespace Demo
{
    static 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:

main.cs(5,19): error CS0106: The modifier `static' is not valid for this item

Explanation:

The above program will generate syntax error because we cannot use static keyword with a structure like this,

static struct Sample

Question 3:

using System;

namespace Demo
{
    struct Sample
    {
        public static int NUM;
        
        public static void Method()
        {
            NUM++;
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {

            Sample.Method();
            Sample.Method();

            Console.WriteLine(Sample.NUM);
        }
    }
}

Output:

2
Press any key to continue . . .

Explanation:

In the above program, we created a structure that contains a static member NUM and a static method Method(), the Method() is used to increase the value of static member NUM in every call. In the Main() method we called Method() two times and then print the value of NUM, then 2 will be printed on the console screen.

Question 4:

using System;

namespace Demo
{
    struct Sample
    {
        public static int NUM;
        
        public static void Method()
        {
            NUM++;
        }
    }
 
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S;
            S.Method();
            S.Method();

            Console.WriteLine(S.NUM);
        }
    }
}

Output:

main.cs(21,15): error CS0176: Static member `Demo.Sample.Method()' 
cannot be accessed with an instance reference, qualify it with a type name instead
main.cs(22,15): error CS0176: Static member `Demo.Sample.Method()' 
cannot be accessed with an instance reference, qualify it with a type name instead
main.cs(24,33): error CS0176: Static member `Demo.Sample.NUM' 
cannot be accessed with an instance reference, qualify it with a type name instead

Explanation:

The above program will generate syntax errors because we cannot static members of a structure using instance reference.

Question 5:

using System;

namespace Demo
{
    const struct Sample
    {
        public static int NUM;
        
        public static void Method()
        {
            NUM++;
        }
    }
 
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {

            Sample.Method();
            Sample.Method();

            Console.WriteLine(Sample.NUM);
        }
    }
}

Output:

main.cs(5,4): error CS1525: Unexpected symbol `const'
main.cs(5,16): error CS1514: Unexpected symbol `struct', expecting `.' or `{'
main.cs(27,0): error CS1525: Unexpected symbol `}'

Explanation:

The above method will generate syntax error because we used the const keyword in structure definition, which is not correct, we cannot use const keyword like this.






Comments and Discussions!

Load comments ↻






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