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

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

        public Sample(int A, int B)
        {
            this.A = A;
            this.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:

The above program will print "30" on the console screen. In the above program, we created a class Sample that contains data members A and B, and we defined a construct and method Print() inside the Sample class.

public Sample(int A, int B)
{
    this.A = A;
    this.B = B;
}

In the constructor of Sample class, here A and B are data members and arguments of the constructor. Here, we used this object to differentiate data members and arguments. this object is used with data members and we can use local arguments directly without using this object.

Question 2:

using System;

namespace Demo
{
    class Sample
    {
        public Sample Method1()
        {
            Console.WriteLine("Method1");
            return this;
        }
        public Sample Method2()
        {
            Console.WriteLine("Method2");
            return this;
        }
        public Sample Method3()
        {
            Console.WriteLine("Method3");
            return this;
        }
    }

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

            S.Method1().Method2().Method3();
        }
    }
}

Output:

Method1
Method2
Method3
Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains three methods, here we implemented cascaded method call by returning this object from every method of Sample class.

S.Method1().Method2().Method3();

We know that we required the object to call the method of the class, here S.Method1() will return object using this then called Method2() and Method2() will also return object then call Method3(), that’s why Method1, Method2, and Method3 will be printed on the console screen.

Question 3:

using System;

namespace Demo
{
    class Sample
    {
        public this Method1()
        {
            Console.WriteLine("Method1");
            return this;
        }
        public Sample Method2()
        {
            Console.WriteLine("Method2");
            return this;
        }
        public Sample Method3()
        {
            Console.WriteLine("Method3");
            return this;
        }

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

            S.Method1().Method2().Method3();
        }
    }
}

Output:

main.cs(7,19): error CS1519: Unexpected symbol `this' in class, struct, 
or interface member declaration
main.cs(7,21): error CS1520: Class, struct, or 
interface method must have a return type

Explanation:

The above program will generate syntax errors. Because we cannot use this object cannot be used as a return type of method. Here, we used this object as the return type in Method1.

public this Method1()
{
    Console.WriteLine("Method1");
    return this;
}

Question 4:

using System;

namespace Demo
{
    class Sample
    {
        public static Sample Method1()
        {
            Console.WriteLine("Method1");
            return this;
        }
        public Sample Method2()
        {
            Console.WriteLine("Method2");
            return this;
        }
        public Sample Method3()
        {
            Console.WriteLine("Method3");
            return this;
        }

    }

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

            S.Method1().Method2().Method3();
        }
    }
}

Output:

main.cs(10,20): error CS0026: Keyword `this' is not valid in a static property, 
static method, or static field initializer
main.cs(32,15): error CS0176: Static member `Demo.Sample.Method1()' 
cannot be accessed with an instance reference, qualify it with a type name instead

Explanation:

The above program will generate syntax errors because of the below method,

public static Sample Method1()
{
    Console.WriteLine("Method1");
    return this;
}

The Method1() is a static method, we cannot use this object inside the static method because static member of a class belongs to the class instead of the object.






Comments and Discussions!

Load comments ↻






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