1

Hi so i have one interface with 2 methods which is implemented by an abstract class and one partial class in 2 separate file which extends the abstract class

Ex:

public inteface Iterface
{
    void A();
    void B();
}

public abstract class Abstract : Interface
{
    public abstract A();
    public abstract B();
}


// Partial1.cs    
public partial class Partial : Abstract
{
    public override A(){}
}

// Partial2.cs
public partial class Partial : Abstract
{
    public override B(){}
}

I get the error that Partial does not implement Abstract correctly, how come? If i would add to Partial1.cs the B() so that i would have

// Partial1.cs
public partial class Partial : Abstract
{
     public override A(){}
     public override B(){}
}

all good, but why i am not allowed to implement the Abstract method in whatever file containing the Partial i want?

I've tried also with the following

// Partial1.cs    
public partial class Partial : Abstract
{
     public override A(){}
}

// Partial2.cs
public partial class Partial
{
    public override B(){}
}

Initially i wanted just to use the Interface but i had the same error "Partial class does not implement Interface correctly" so i though that maybe i cannot do so.

Is there any way of doing what i'm trying here? Implement the methods in whichever Partial class file i want ?

Thank you

10
  • Whatever the problem is, I think it's worth remarking that using partial classes is considered really bad programming practice. The reason partial classes exist at all, is that they work well for separating your own source from automatically generated source from design time tools. Other than that, don't use them. If you find that you are creating really large classes, then it's time to look at your design. Commented May 27, 2017 at 8:36
  • @BentTranberg I have to disagree with you. Sometimes partials are extremely useful in breaking down a class, especially when working in teams. It does not really associate with size of the class, more cases than not it's main driver is readability and organization. Commented May 27, 2017 at 8:39
  • The reason why i'm using partial class is to create a repository for one Model. So i would have an IModelInfo which contains numerous information about a Model, and than IModelCreation which contains numerous ways of creating a Model (Model is the same Entity for example a Student). I want to have a clear separation of the actions and have a clean way of looking into the code. The reason for why i'm trying this approach is for a better application maintainability. Commented May 27, 2017 at 8:49
  • 1
    @XulescuXulescuXulesc What IDE are you using? Commented May 27, 2017 at 9:31
  • 1
    I would be surprised if it was something so dumb, but all your method declaration miss the word "void". Apart that, your code seems to be valid. What version of C# do you use, and what error do you get exactly ? Commented May 27, 2017 at 9:43

2 Answers 2

3

Short Answer:

It depends on your compiler/IDE.
For example, it is possible in some versions of Visual Studio you may not be able to break up an interface across multiple partial classes.


You can do it.

In LINQPad and VS2017 this will compile just fine...

public interface I1
{
    void A();
    void B();
}

partial class Foo : I1
{
    public void A() {}
}

partial class Foo : I1
{
    public void B() {}
}

void Main()
{
    Foo foo = new Foo();
    foo.A();
    foo.B();
}

Consider using an alternative design

The alternative and viable use-case for partial classes would be to break down your class against different multiple interfaces. For example:

Interfaces

public inteface I1
{
    void A();
}

public inteface I2
{
    void B();
}

Breaking up a class 1-1 with Interfaces

public partial class Partial : I1
{
    public void A() { }
}

public partial class Partial : I2
{
    public void B() { } 
}

Which ultimately would compile to

public partial class Partial : I1, I2
{
    public void A() { }
    public void B() { } 
}
Sign up to request clarification or add additional context in comments.

1 Comment

yes, I know that i could use 2 interfaces but this is not what i want. what i want is, having an interface or abstract class with multiple methods, and a partial class that inherits or extends from one of the previous mentioned, i want to know if it is possible to implement one method in one part of the partial class and the 2nd method in the other part of the partial class.
0

Found reason why it did not worked.

A partial class defined in assembly a.b.c is not the same as defined in assembly a.b

So i did not care to look about assembly info, in order for my partial class to have worked as i wanted i had to add them in the same assembly.

D'oh!

EX:

namespace A.BL.Repository.ARepo 

should be used wherever the partial class is present

3 Comments

Based on the information provided in your question, there would be no way to give you this answer. :)
yes i know. it was an honest mistake, actually i have no idea why the assemblies were different since i've created a file and copy paste it and only change the file name. reason for why i didn't even bother to care about
well with the missing information you still provided a good answer from my point of view, and only though about the reason for why the compiler is not able to bind the two after i've saw the comment referring to different compilers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.