Graviton
Graviton

Reputation: 83254

Partial Interface in C#

Does C# allows partial interface? i.e., in ManagerFactory1.cs class, I have

public partial interface IManagerFactory
{
    // Get Methods
    ITescoManager GetTescoManager();
    ITescoManager GetTescoManager(INHibernateSession session);
}

and in ManagerFactory.cs class, I have:

public partial interface IManagerFactory
{
    // Get Methods
    IEmployeeManager GetEmployeeManager();
    IEmployeeManager GetEmployeeManager(INHibernateSession session);
    IProductManager GetProductManager();
    IProductManager GetProductManager(INHibernateSession session);
    IStoreManager GetStoreManager();
    IStoreManager GetStoreManager(INHibernateSession session);
}

Both ManagerFactory and ManagerFactory1 are located in the same assembly.

Upvotes: 65

Views: 41467

Answers (9)

ShuggyCoUk
ShuggyCoUk

Reputation: 36438

See the docs which state that it can be used on classes, structs or interfaces.

Upvotes: 2

Krzysztof Kozmic
Krzysztof Kozmic

Reputation: 27374

Think twice before making your interface partial. Maybe it's better to split it into two interfaces?

Keep your interfaces small and focused. partial is a code smell.

Upvotes: 5

Konstantin Tarkus
Konstantin Tarkus

Reputation: 38378

Yes, it does.

Partial Classes and Methods (C# Programming Guide) at MSDN

Restrictions:

  • All partial-type interface definitions meant to be parts of the same type must be modified with partial.
  • The partial modifier can only appear immediately before the keyword interface.
  • All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file).

Partial interfaces are primary used when code generation is involved. For example when one part of an interface is generated and the other one is user-written.

Upvotes: 9

Kim
Kim

Reputation: 39

Nice.

I agree this could be a smell, but I can still think of one reason to do it.

I'm currently working on an application MVVM framework for WPF and Silverlight. What I've encountered is that WPF and Silverlight are so different that rather than using defines all over the code, the partial interface/class can actually separate the differences between the two frameworks and still keep the code clean and nearly single sourced.

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1500835

The simplest way is just to try it :)

But yes, partial interfaces are allowed.

Valid locations for the partial modifier (with C# 3.0 spec references):

  • Classes (10.1.2)
  • Structs (11.1.2)
  • Interfaces (13.1.2)
  • Methods (C# 3.0+) (10.2.7; 10.6.8)

Section 10.2 of the spec contains most of the general details for partial types.

Invalid locations:

  • Enums
  • Delegates

Upvotes: 102

Kent Boogaart
Kent Boogaart

Reputation: 178680

Yes, it does. Are both partial interfaces defined in the same namespace?

Upvotes: 2

Keith
Keith

Reputation: 155692

It does, but an important question would be why?

Partial classes are there so that you can extend auto-generated code. VS can generate a form file, or code behind, or Linq to SQL accessor and you can extend it using a partial.

I'd avoid using partials just to split up classes (or in this case interfaces) as generally that generates more confusion than it's worth.

In this case I'd investigate why this needs to be across multiple files - factory pattern interfaces can make tracking back through you code more complex, but here you'd be tracking back through multiple files.

Upvotes: 7

Michael Piendl
Michael Piendl

Reputation: 2884

Short and simple: YES!

Upvotes: 3

Anton Gogolev
Anton Gogolev

Reputation: 115749

Yes, it does.

Upvotes: 6

Related Questions