Q:

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

belongs to collection: C#.Net find output programs

0

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.

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

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

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

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

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

All Answers

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

Answer1:

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.

Answer 2:

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.

Answer  3:

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.

Answer 4:

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.

Answer 5:

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#.

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 (static Keyword) | set... >>
<< C#.Net find output programs (this Keyword) | set 1...