Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
C#
public class classA
{

    public void MethodA()
    {
        Console.Write("Raised in MethodA");
    }
    public void MethodB()
    {
        Console.Write("Raised in MethodB");
    }
}



I want to access these two methods like
C#
(new classA()).MethodA().MethodB();


Is it possible Extension Methods are working like that y not normal Class
Posted
Updated 1-Feb-15 6:23am
v2
Comments
[no name] 1-Feb-15 12:32pm    
And why you like to do it in this way? In case MethodB needs to executed everytime MethodA is called, then it is the MethodA's Job to do it. Otherwise it is only worthless syntactic sugar....but maybe you will explain more what you try to do.
Sergey Alexandrovich Kryukov 1-Feb-15 21:07pm    
Exactly. Well, there are many cases when such usage is practical, but in much less trivial cases.
By the way, extension methods are something very different, I would advise OP to read about them.
—SA

1.The class name should be with uppercase: "ClassA";

2.Your code will not work, in order to can work your method "MethodA" have to return your class "ClassA", so in your method you should have the next code: "return this;" to return the current object.
 
Share this answer
 
Comments
JagadishBB 1-Feb-15 12:34pm    
Thank You ...
Raul Iloc 1-Feb-15 12:36pm    
Welcome, I am glad that I could help you!
The expression you provided doesn't work (also not for extension methods), unless MethodA returns a classA, but in this case, it doesn't -- its return type is void.

I don't know why you want to call them that way. I'd suggest to store the new classA in a variable, and call the methods like this:
C#
classA a = new classA();
a.MethodA();
a.MethodB();

If you really want to use that expression, make MethodA return this, which has classA as type. (and perhaps MethodB should do the same, if you want to make your expression longer):
C#
public class classA
{
    public classA MethodA()
    {
        Console.Write("Raised in MethodA");
        return this;
    }
    public classA MethodB()
    {
        Console.Write("Raised in MethodB");
        return this;
    }
}

With the above class, your expression works.
 
Share this answer
 

You can achieve that goal by returning the object's instance, instead of void. Something like:


C#
public class classA
{
 
    public classA MethodA()
    {
        Console.Write("Raised in MethodA");

        return this;
    }
    public classA MethodB()
    {
        Console.Write("Raised in MethodB");

        return this;
    }
}
 
Share this answer
 
Well, the answer is "yes, you could do that, but why would you want to do that ?" Let me see if I can "talk you out of that."
C#
using System;
using Feb1_ExtendClass;

namespace Feb1_ExtendClass
{
    public class ClassA
    {
        public int MethodA(int value)
        {
            Console.Write("Raised in MethodA");
            return value + value;
        }

        public int MethodB(int value)
        {
            Console.Write("Raised in MethodB");
            return value/2;
        }
    }
}

namespace ExtensionMethods
{
    public static class ExtendsA
    {
        public static ClassA MethodB(this ClassA a)
        {
            Console.WriteLine("Raised in Extension MethodB");
            return a;
        }

        public static ClassA MethodA(this ClassA a)
        {
            Console.WriteLine("Raised in Extension MethodA");
            return a;
        }
    }
}
Put to the test in a WinForms project:
C#
using System;
using System.Windows.Forms;
using ExtensionMethods;

namespace Feb1_ExtendClass
{
    public partial class Form1 : Form
    {
        public Form1() {InitializeComponent();}

        private void button1_Click(object sender, EventArgs e)
        {
            ClassA newA = new ClassA();

            var whatever = newA.MethodA().MethodB();
        }
    }
}

//The result in the variable 'whatever will be the same instance of ClassA you started with:
//? whatever == newA
//true
What I hope you can see in this strange example is that:

1. there's no point writing an Extension Method for a Class if the method doesn't change something in the Class instance it operates on.

With rare exceptions (damn if I can think of any), you should:

1. write Extension methods that take parameters, and return some useful results.

Note: I deliberately made this example as weird as possible by putting methods in ClassA with the same names as the Extension Methods, but with different structure, for the purpose of showing the semantic independence of Extension Methods from Class Methods. This is a practice I would never hope to see done in actual code !
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900