Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning itself in property

Tags:

c#

.net

I've start using following method grouping to make my code more readable:

public interface IUserMethodGroup
{
   void SomeMethodOne();
   String SomeMethodTwo();
}

//File MainClass.Users.cs
public partial class MainClass : IUserMethodGroup
{
   void IUserMethodGroup.SomeMethodOne(){ //some code }
   String IUserMethodGroup.SomeMethodTwo(){ return "some str";}
}

//File MainClass.cs
public partial class MainClass
{
    //HOW ABOUT PERFORMANCE?
    public IUserMethodGroup UserHandle {get { return this; } }       
}

Does it has big impact on performance?

Edit 1

This allow me to do that:

class ConsumerClass
{
   public void Method()
   {
      MainClass mc = new MainClass();

      mc.UserHandle.SomeMethodOne();

      //@ vc 74:
      //Interface has to be explicitly, otherwise it will be:
      ms.SomeMethodOne(); //grouping lost...
    }
}

@adelphus: Consider I have 5 similar properties (method groups). Every time I want to use some method from group I asking class to return itself. Does it be much slower compared to implementing without groups?

like image 559
Artur Michajluk Avatar asked Nov 09 '22 07:11

Artur Michajluk


1 Answers

Every time I want to use some method from group I asking class to return itself. Does it be much slower compared to implementing without groups?

No. Returning this in a read-only property should just be one line of IL code. In fact is may even be inlined by the JIT optimizer.

The bigger question is why do you have so many methods on one class? Having so many methods that they need to be "organized" by explicit interface implementations smells like you have a class that is doing too much.

One thing this design does allow is for you to have different classes that implement the different "groups". Your single class then becomes an aggregator that combines the functionality of several different interfaces into a single class.

like image 163
D Stanley Avatar answered Nov 14 '22 23:11

D Stanley