Q:

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

belongs to collection: C#.Net find output programs

0

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

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

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

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

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

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

All Answers

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

Answer 1:

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.

Answer 2:

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

Answer 3:

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.

Answer 4:

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.

Answer 5:

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.

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 (Classes & Objects) | ... >>
<< C#.Net find output programs (Structure) | set 2...