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

Find the output of C#.Net programs | readonly Keyword | Set 1: Enhance the knowledge of C#.Net readonly Keyword concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 05, 2021

Question 1:

using System;

namespace Demo
{
    class Sample
    {
        private readonly int A;
        private readonly int B;

        public Sample()
        {
            A = 10;
            B = 20;
        }

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

Output:

30
Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains two readonly data members A and B that are initialized with values 10 and 20 respectively in the default constructor of the class. Here, we also defined a method Print() to print the sum of A and B on the console screen.

Now look to the Main() method of Program class. Here, we created object S of Sample class and then call Print() method using object S that will print "30" on the console screen.

Question 2:

using System;

namespace Demo
{
    class Sample
    {
        private readonly int A;
        private readonly int B;

        public Sample()
        {
            A = 10;
            B = 20;
        }

        public readonly void Print()
        {
            Console.WriteLine(A + B);
        }
    }

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

Output:

main.cs(16,30): error CS0106: The modifier `readonly' is not valid for this item

Explanation:

The above program will generate syntax error because here we defined the Print() method with a readonly keyword, but we cannot use the readonly keyword in the method definition.

Question 3:

using System;

namespace Demo
{
    class Sample
    {
        private readonly int A;
        private readonly int B;

        public Sample()
        {
            A = 10;
            B = 20;
        }

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

Output:

main.cs(27,12): error CS1525: Unexpected symbol `readonly'

Explanation:

The above program will generate a syntax error because of the below statement,

readonly Sample S = new Sample();

We cannot use the readonly keyword for object creation.

Question 4:

using System;

namespace Demo
{
    public struct Sample
    {
        private readonly int A;
        private readonly int B;

        public Sample(int a, int b)
        {
            A = a;
            B = b;
        }

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

Output:

30
Press any key to continue . . .

Explanation:

In the above program, we created a structure Sample. The Sample structure contains two readonly data members that are initialized with the parameterized constructor and the Sample class also contains a Print() method that will print the sum of data members A and B on the console screen.

Here, we defined Main() method inside the Program class, The Main() method is the entry point of the program, here we created reference S of structure Sample and initialized data member A and B with 10 and 20 respectively. Then we called the Print() method using reference S that will print "30" on the console screen.

Question 5:

using System;

namespace Demo
{
    public struct Sample
    {
        private readonly int A;
        private readonly int B;

        public Sample()
        {
            A = 10;
            B = 20;
        }

        public void Print()
        {
            Console.WriteLine(A + B);
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S = new Sample(10,20);
            S.Print();
        }
    }
}

Output:

main.cs(10,16): error CS0568: Structs cannot contain 
explicit parameterless constructors

Explanation:

The above program will generate syntax error because as we know that a structure cannot contain a no-argument or default constructor in C#.





Comments and Discussions!

Load comments ↻





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