-2
public class Car
{
    ......
}

is it require to use parent class each time in all partial classes or just first one (and why? good for performance or clean code?)

public partial class TwoWheels : Car
{
     ..... (1)
}

public partial class TwoWheels : Car
{
     ..... (2)
}

Or

public partial class TwoWheels : Car
{
     ..... (1)
}

public partial class TwoWheels
{
     ..... (2)
}

i'm using second one in my codes but first one is working too

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • _"is it require to use parent class each time in all partial classes"_ -- you already know it's not, because you tried it without the additional inheritance declaration and it worked. The rest of your question is pure opinion. You may redeclare the inheritance or not, as is your preference. See also https://stackoverflow.com/questions/21794381/if-a-partial-class-inherits-from-a-class-then-all-other-partial-classes-with-the – Peter Duniho Jul 09 '17 at 22:31

2 Answers2

2

No, you need to define the Inheritance relationship only once as you posted in question (below one); since it's a partial class by definition and thus when the merging happens you anyways have the inheritance definition set up properly

public partial class TwoWheels : Car
{
     ..... (1)
}

public partial class TwoWheels
{
     ..... (2)
}

Quoting From Documentation

If any part declares a base type, then the whole type inherits that class. All the parts that specify a base class must agree, but parts that omit a base class still inherit the base type. The final type is the combination of all the parts at compile time.

Example:

partial class Earth : Planet, IRotate { }
partial class Earth : IRevolve { }

Equivalent to

class Earth : Planet, IRotate, IRevolve { }
Rahul
  • 76,197
  • 13
  • 71
  • 125
0

Yep, you only "need" it once - when the partial class is bundled up, it'll use that inheritance for the full class.

But... before you go too far down this line, make sure you read up on why partial classes exist.

When is it appropriate to use C# partial classes?

... basically, chances are you shouldn't be using them. Personally, every time I've written "public partial class"... I later had to redo the code to use base/derived classes to do the job better and more cleanly.

Kevin
  • 2,133
  • 1
  • 9
  • 21