4

I was reading the MSDN article on the Partial keyword, and this part caught my eye:

The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private, and so on.

[...]

All the parts that specify a base class must agree, but parts that omit a base class still inherit the base type. Parts can specify different base interfaces, and the final type implements all the interfaces listed by all the partial declarations. Any class, struct, or interface members declared in a partial definition are available to all the other parts. The final type is the combination of all the parts at compile time.

I had two questions regarding this concept:

  • Firstly, it seems that this is a way to bypass the lack of multiple inheritance in C# (aside from interfaces, of course). Are there any repercussions to doing so, aside from normal multiple inheritance issues, such as the Diamond Problem? Basically, just because I can, does it mean I should?

  • Secondly, when exactly should I split up files? Just by reading this, it feels like I should be able to declare a nested class in its own file, and partial it together with the containing class, and thereby improve readability. Is this the point of Partial, or should it only be used as described in the above article?

1
  • 1
    Partial classes are mostly a bad thing IMHO, because you might have to jump between different files to see all the code.
    – Felix K.
    Jan 19, 2013 at 20:47

5 Answers 5

8

Firstly, this has nothing to do with multiple inheritance in C#. It simply allows you to split a class' implementation between files. A regular class in C# can implement multiple interfaces as well, so you're not gaining anything by using partial classes.

Secondly, partial classes are the most useful when part of a class' implementation is generated by some tool and the other portion is written by a developer. This allows you to re-generated the generated portion of the code without losing the hand coded portion of the implementation.

2

partial classes have nothing to do with multiple inheritance. the only very good reason to use them is when part of the class is generated and the other is your own.

2

No, it mentions base interfaces, not concrete types, so multiple inheritance still isn't possible. For your second point, the main reason was to allow generation tools to create a partial which hides many of the implementation details from the partial that the developer edits.

1
  • Whoops, you're absolutely right! It says it right there in the bold type I quoted.
    – Kyle Baran
    Jan 19, 2013 at 21:11
1

Firstly, it seems that this is a way to bypass the lack of multiple inheritance in C# (aside from interfaces, of course). Are there any repercussions to doing so, aside from normal multiple inheritance issues, such as the Diamond Problem? Basically, just because I can, does it mean I should?

It doesn't do multiple inheritance. It's actually way more evil, since it exposes private variables -- but at the same time it doesn't introduce diamonds. Try the following code to see what I mean:

public class Test0
{
    protected int bar;

    public void Unexpected() { Console.WriteLine("3. {0}", bar); }
}

public partial class Test1
{
    private int foo;

    public void Foo() { Console.WriteLine("1. {0}", foo); bar = 1; }
}

public partial class Test1 : Test0
{
    public void Bar() { Console.WriteLine("2. {0}", foo); foo = 1; }
}

class Driver
{
    public static void Main()
    {
        var t1 = new Test1();
        t1.Bar();
        t1.Foo();
        t1.Unexpected();
        Console.ReadLine();
    }
} 

In other words, you should be very careful with variables.

Secondly, when exactly should I split up files? Just by reading this, it feels like I should be able to declare a nested class in its own file, and partial it together with the containing class, and thereby improve readability. Is this the point of Partial, or should it only be used as described in the above article?

Code generation is a well known example. I personally use partial classes a lot when dealing with the Facade pattern (which is very useful when creating WCF/SOAP services). In most cases I try to avoid it for the above reason.

3
  • 1
    The variables aren't necessarily exposed. The first declaration of Test1 should most certainly have access to Test0's protected variables, as the combined version of Test1 eventually inherits from Test0. Now, whether this was intended or not is up for the programmer to decide.
    – Kyle Baran
    Jan 19, 2013 at 22:37
  • 1
    I tried to show how scope works when working with partial. The point was that both local (in-class) and protected (from inheritance) variables are accessible, even though the other partial class inherits the base class. It is very possible that at some point you want to use the base class protected variable, while you're actually working with the local variable declared in the partial class. The workaround I usually use is simply using as little variables as possible and not using inheritance when working with partial classes.
    – atlaste
    Jan 21, 2013 at 9:02
  • I don't understand your example. I guess it shows that something works different than expected? Could you add/say what was "expected" and what really happened? (e.g. So that readers can get it at first glance without having to run the code.)
    – Pablo H
    Jun 21, 2019 at 13:13
-2

The partial keyword in C# is a feature that allows to split the definition of a class, struct, interface, or method over two or more source files1. This can be particularly useful.

When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time. Code can be added to the class without having to recreate the source file. When using source generators to generate additional functionality in a class

The partial keyword can make code more organized and manageable, especially when working with large classes or when want to separate auto-generated code from own code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.