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

Find the output of C#.Net programs | static Keyword | Set 2: 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 Sample
    { 
        static int VAL;
        public 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:

main.cs(8,23): error CS0515: `Demo.Sample.Sample()': 
static constructor cannot have an access modifier

Explanation:

The above program will generate syntax error because we cannot use access modifiers with static constructors in C#.

Question 2:

using System;

static namespace Demo
{
    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(3,7): error CS1525: Unexpected symbol `namespace'

Explanation:

The above program will generate syntax error here we tried to create the static namespace, which is not possible. We cannot create a static namespace in C#.

Question 3:

using System;

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

        static ~Sample()
        {
            Console.WriteLine("Destructor called");
        }

        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(13,17): error CS0106: The modifier `static' is not valid for this item

Explanation:

The above program will generate syntax error because we cannot define static destructor in C#.





Comments and Discussions!

Load comments ↻





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