63

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.

Graviton
  • 81,782
  • 146
  • 424
  • 602

9 Answers9

97

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
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Partial methods are very interesting - they allow you to create a function prototype in one half of a partial class and add a method implementation in the other half. – Andrew Hare Apr 09 '09 at 13:09
  • 1
    @Stormenet Sounds like a reeeeelllllyyyyy bad idea. The (abuse):(legitimate) ration has got to be astronomical. – Michael Meadows Apr 09 '09 at 13:09
  • If someone is interested to know more about partial methods: http://community.bartdesmet.net/blogs/bart/archive/2007/07/28/c-3-0-partial-methods-what-why-and-how.aspx – Stormenet Apr 09 '09 at 13:10
  • 10
    @Michael This is usually done in codegen'd code. This allows to modify the behavior of generated classes without inheriting from them and thus without the overhead of calling virtual functions. – Anton Gogolev Apr 09 '09 at 13:11
  • 2
    @Michael, I'm not planning to use it, but knowing the existence of it is good :) – Stormenet Apr 09 '09 at 13:12
  • @Michael Meadows - the only place I've actually seen it used is in code generated by the designer. LINQ-to-SQL, for example, will partial describe methods such as On{Property}Changed which lets you easily hook into the life cycle of your object. – Matt Apr 09 '09 at 13:12
  • @Anton Gogolev, It sounds less scary than I initially imagined, but I can still see overeager "architects" doing some very bad things with them. :@ – Michael Meadows Apr 09 '09 at 13:13
  • 1
    @Michael Meadows, partial methods are always private, so the abuse should be limited :) – Stormenet Apr 09 '09 at 13:15
  • 1
    Partial methods are simply one more tool available to you, I wouldn't worry too much about their "(abuse):(legitimate) ration". The "(abuse):(legitimate) ration" of any tool is directly proportional to the creativity of the abuser (in other words someone can wreak havoc with anything). – Andrew Hare Apr 09 '09 at 13:16
  • The point of partial methods is that they get optimised out if they aren't used (unlike empty virtual methods). If you want auto-generated code to call hand-written methods they're just the ticket. – Keith Apr 09 '09 at 16:58
  • I use partial functions in code that I myself have written the T4 templates to autogenerate. I have a lot of classes that have to do similar tasks, but in some of them, there are exceptions. Rather than put the exceptions in the T4 template, I define a partial method and call the partial method from the code for every class being created. I only actually implement the partial method for those classes that actually need to do something different. – Hank Schultz Jun 11 '14 at 13:07
9

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Konstantin Tarkus
  • 37,618
  • 14
  • 135
  • 121
7

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.

Keith
  • 150,284
  • 78
  • 298
  • 434
  • 8
    I normally won't design to have partial classes and interfaces too. But I got the classes and interfaces from code generation. So I have to use partial to accommodate the code – Graviton Apr 09 '09 at 13:29
  • Fair enough - though I'd be careful with generated interfaces - if they change your implementations of them are likely to break. – Keith Apr 09 '09 at 16:55
  • I have found the partial modifier is great when you are working on a team and using source control (SVN in our case). We setup things like service or helper classes which contain a lot of methods and put each one in a separate cs file using partial modifier. This allows us to check in only the changed methods file and provide a better history of changes to that one piece of code. It also reduces the conflict during checkin. – Frank Feb 09 '12 at 17:21
  • 2
    @Frank I've never needed that - merging works fine when developers are working on different methods in different parts of the file, and the *blame* feature tells you exactly who edited each line. I've found that to be the case with both SVN and TFS. However your strategy would be useful with a check-out/check-in model like VSS or Vault. – Keith Feb 10 '12 at 08:07
  • @Keith Sometimes it's good to separate interface into different parts by using "partial". For example, when the content is too long to read even though you have used the #region hashtag to separate them. – Tony Wu Jul 14 '16 at 04:34
6

Yes, it does.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
4

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115
3

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.

InteXX
  • 6,135
  • 6
  • 43
  • 80
Kim
  • 39
  • 1
3

Short and simple: YES!

Michael Piendl
  • 2,864
  • 1
  • 27
  • 21
2

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

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
2

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

Pang
  • 9,564
  • 146
  • 81
  • 122
ShuggyCoUk
  • 36,004
  • 6
  • 77
  • 101