What is the advantage of partial classes over inheritance?
Asked Answered
P

4

5

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?

Practiced answered 24/8, 2014 at 2:33 Comment(3)
Access to private members (eg, WinForms)Mauser
partial classes and inheritance are separate concepts. Partial classes are mainly there to dovetail with code generationPhonography
@Mauser cant you just make it protected in that case?Practiced
S
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.

Supersensitive answered 24/8, 2014 at 2:43 Comment(4)
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?Practiced
@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.Supersensitive
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.Practiced
@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.Biographer
C
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.

Clothespin answered 24/8, 2014 at 2:40 Comment(4)
-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.Practiced
@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.Clothespin
You could do the same thing with Regions. I dont consider that a realistic advantage.Practiced
@Dgrin91 No, you cannot. #region is just a toggle section of code in one file.Clothespin
D
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.

Donor answered 24/8, 2014 at 2:45 Comment(0)
D
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
 }
Dichotomize answered 24/8, 2014 at 3:37 Comment(2)
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 you will be able to comment on any post.Vogue
I think above code is possible to know advantage over inheritance@DanNeelyDichotomize

© 2022 - 2024 — McMap. All rights reserved.