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

Find the output of C#.Net programs | static Keyword | Set 1: Enhance the knowledge of C#.Net static Keyword 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
{
    class Program
    {
        int A;
        int B;

        Program()
        {
            A = 10;
            B = 20;
        }
        static void MyMethod()
        {
            int C = 0;

            C = ++A + B++;

            Console.WriteLine(A + " " + B + " " + C);
        }
        //Entry point of the program
        static void Main(string[] args)
        {
            MyMethod();
        }
    }
}

Output:

main.cs(19,19): error CS0120: An object reference is required to access non-static member `Demo.Program.A'
main.cs(19,23): error CS0120: An object reference is required to access non-static member `Demo.Program.B'
main.cs(21,31): error CS0120: An object reference is required to access non-static member `Demo.Program.A'
main.cs(21,41): error CS0120: An object reference is required to access non-static member `Demo.Program.B'

Explanation:

In the above program, we created two data members A and B, and we defined a static method MyMethod(), and we are accessing A and B inside static method MyMethod(), but we cannot access non-static members of class inside the static method. That's why the above program will generate syntax errors.

Question 2:

using System;

namespace Demo
{

    class Sample
    { 
        static int VAL;
        Sample()
        {
            VAL++;
        }

        static void ObjectCount()
        {
            Console.WriteLine(VAL);
        }
    }
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S1 = new Sample();
            Sample S2 = new Sample();
            Sample S3 = new Sample();

            Sample.ObjectCount();
        }
    }
}

Output:

main.cs(24,25): error CS0122: `Demo.Sample.Sample()' is inaccessible due to its protection level
main.cs(9,9): (Location of the symbol related to previous error)
main.cs(25,25): error CS0122: `Demo.Sample.Sample()' is inaccessible due to its protection level
main.cs(9,9): (Location of the symbol related to previous error)
main.cs(26,25): error CS0122: `Demo.Sample.Sample()' is inaccessible due to its protection level
main.cs(9,9): (Location of the symbol related to previous error)
main.cs(28,20): error CS0122: `Demo.Sample.ObjectCount()' is inaccessible due to its protection level
main.cs(14,21): (Location of the symbol related to previous error)

Explanation:

The above program will generate syntax error because in the above program constructor Sample() and static method ObjectCount(), both are private that cannot be accessed outside the class, but we accessed constructor of Sample class and method ObjectCount() in the Main() method of Program class.

Question 3:

using System;

namespace Demo
{

    class Sample
    { 
        static int VAL;
        public Sample()
        {
            VAL++;
        }

        public static void ObjectCount()
        {
            Console.WriteLine(VAL);
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S1 = new Sample();
            Sample S2 = new Sample();
            Sample S3 = new Sample();

            Sample.ObjectCount();
        }
    }
}

Output:

3
Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains a static member VAL. Here, we increase the value of data member VAL by 1 in the constructor of Sample class.

Now, look at the Main() method of class Program. Here, we created three objects of Sample class. As we know that static data member of the class is initialized with 0. We created three objects that's why data member VAL increased three times. Then we print the count of created objects using the ObjectCount() method on the console screen.

Question 4:

using System;

namespace Demo
{
    static class Sample
    { 
        int VAL;
        public Sample()
        {
            VAL=10;
        }

        public void Print()
        {
            Console.WriteLine(VAL);
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S = new Sample();

            S.Print();
        }
    }
}

Output:

main.cs(7,13): error CS0708: `Demo.Sample.VAL': cannot declare instance members in a static class
main.cs(8,16): error CS0710: `Demo.Sample': Static classes cannot have instance constructors
main.cs(13,21): error CS0708: `Demo.Sample.Print()': cannot declare instance members in a static class

Explanation:

The above program will generate syntax error because a static class can contain only static members, here Sample is a static class that contains non-static members.

Question 5:

using System;

namespace Demo
{
    class Sample
    { 
        static int VAL;
        static Sample()
        {
            VAL=10;
        }

        public void Print()
        {
            Console.WriteLine(VAL);
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S = new Sample();

            S.Print();
        }
    }
}

Output:

10
Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains static data member VAL, which is initialized in the static constructor with 10. Class Sample also contains a non-static method Print(), which is used to print the value of VAL on the console screen.

Now look the Main() method, here we created an object Sample class, then static constructor called and initialized value of VAL by 10, and then print the value of VAL on the console screen.





Comments and Discussions!

Load comments ↻





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