Returning to ownership

I started using the following grouping of methods 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 this affect performance significantly?

Change 1

This allows me to do this:

 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: I have 5 similar properties (method groups). Every time I want to use a method from group I, I return to the class. Is it much slower compared to a groupless implementation?

+5
source share
1 answer

Every time I want to use a method from a group, I ask the class to return. Is it much slower compared to a groupless implementation?

No. Returning this to a read-only property should be only one line of IL code. In fact, it can even be built in by the JIT optimizer.

The bigger question is, why do you have so many methods in one class? Having so many methods that you need to โ€œorganizeโ€ them with explicit implementations of the interface, it smells as if you have a class that does too much.

One thing that this design allows is to have different classes that implement different โ€œgroupsโ€. Then your single class becomes an aggregator that combines the functionality of several different interfaces into one class.

+1
source

All Articles