4

C# has the concept of partial classes. One instance I've seen this used is in WSDLs. Visual Studio could contact a server to find a service, and automatically generate a partial class based on it. Visual Studios would then provide you with a blank partial class to match it so that you can add your own code.

I feel this approach is rather confusing. Is there any advantage to partial classes over inheritance?

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122

4 Answers4

8

Partial classes are there to address a particular issue - solving a problem of separating generated and hand-programmed code. Without partial classes programmers would need to either refrain from modifying generated classes, or rely on a design pattern to achieve the separation.

One very important point is that generated portions of partial classes have implementation. This sets them apart from interfaces, which do not contain implementation.

In a sense, this makes them similar to an abstract class, without making them abstract. You are allowed to extend and alter functionality without subclassing it.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I still dont understand why we cant generate a super class and work with a subclass. We can expose everything through inheritance in essentially the same way as we would a partial class. What would be the loss in generating a super class that we shouldn't touch? – David says Reinstate Monica Aug 24 '14 at 02:45
  • 1
    @Dgrin91 Then you would need a mechanism to say what subclass you want the system to use as an implementation. In addition, it would create a false impression that we've generated a base class built for inheritance, rather than saying that it's a single monolithic class that it is. In some cases generating base classes would double the number of classes in the system. Partial class provides an elegant and simple solution to this issue. – Sergey Kalinichenko Aug 24 '14 at 02:50
  • There is already a mechanism to say what class you want the system to use for the partial class (ie the name of the service), so what would be wrong with autogenerating a superclass? IE say we have a service called `Authentication`. Currently, an `Authentication` partial class would be made. Through inheritance, an empty `Authentication` class would be made and would inherit from `AbstractAuthentication` that is auto-generated. I also dont think doubling the number of classes is a serious issue (though I could be wrong), because the total code is essentially the same. – David says Reinstate Monica Aug 24 '14 at 02:56
  • 1
    @Dgrin91 C# has a limitation that it may only inherit from one class at a time, if you inherit `AbstractAuthentication`, then you would not be able to inherit from something else. Having an abstract class would also let you have multiple sub-classes, which may not be desirable. It also pollutes your namespace with classes that you would never reference directly, for example, a method that accepts `AbstractAuthentication` as an argument may not make sense in any scenario. – Matthew Aug 24 '14 at 03:17
2

Partial class:
You can define a class in more than one file in a same project. You might end up create a file that contains method, another file contains properties, and so on. At compile time, it will be the same as you create one large file that contains everything.

Read more about partial class and method

Inheritance:
You can extend functionality of existing class both within the same project or on another project. With inheritance, you can extend function, feature, etc. of existing class on a new class.

Ekk
  • 5,627
  • 19
  • 27
  • 1
    -1 because this doesnt answer my question. I know what the difference is, I dont understand why partials is advantageous over inheritance in this case. – David says Reinstate Monica Aug 24 '14 at 02:49
  • @Dgrin91 As I said on my answer, with partial class you can keep code organized. You can create one file that contains only methods, another file that contains only properties. When you want to check properties on a class, you can go to that file which only have properties but nothing else. – Ekk Aug 24 '14 at 02:53
  • You could do the same thing with `Region`s. I dont consider that a realistic advantage. – David says Reinstate Monica Aug 24 '14 at 02:55
  • @Dgrin91 No, you cannot. #region is just a toggle section of code in one file. – Ekk Aug 24 '14 at 14:14
0

Agreed partial classes are much more about splitting a class definition among multiple source files than inheritance.

It always seemed to me that the main reason for them was so that frameworks could generate boiler-plate code for you and you could also add methods without your code being overridden by the stuff visual studio created for data access or web service generation.

It always seemed wrong-headed and lazy of Microsoft to me.

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
0

this is an invitation to the developer to implement method if desired in a second declaration for partial class You can maintain your application better by compacting large classes. Suppose you have a class that has multiple interfaces so you can create multiple source files depending on interface implements. It is easy to understand and maintain an interface implemented on which the source file has a partial class. Let's see the following code snippet.

  public interface IRegister
  {
//Register related function
  }

  public interface ILogin
  {
//Login related function
  }

  //UserRegister.cs file
 public partial classUser : IRegister, ILogin
  {
//implements IRegister interface
  } 

 //UserLogin.cs file
 public partial classUser
 {
//implements ILogin interface
 }
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Dan Is Fiddling By Firelight Aug 24 '14 at 04:06
  • I think above code is possible to know advantage over inheritance@DanNeely –  Aug 24 '14 at 06:36