[antlr-interest] Use partial classes for C#

Dr. Hartmut Kocher hwk.cortex-brainware at t-online.de
Wed Mar 28 09:57:47 PDT 2007


Hi Martin,

The main difference is that by using an abstract class, you can only
override features that have been exposed as being public or protected in the
base class. So you might end up making things protected that would be
private otherwise. This might break encapsulation.

A partial class is one class, split into several files. Therefore, you can
access private members. So you don't have to give up encapsulation just to
add new features. So you have the benefit that code generators are simpler
because they only regenerate complete files and don't have to check if
something was added manually. You still have the ability to add
functionality to the class (in a different file) including access to
otherwise private members.

This is used in .NET 2.0 all over the place (visual designers, XML
serializers, etc.)

Hope this helps.

Regards

Dr. Hartmut Kocher

-----Ursprüngliche Nachricht-----
Von: Martin Probst [mailto:mail at martin-probst.com] 
Gesendet: Mittwoch, 28. März 2007 17:55
An: hwk at cortex-brainware.de
Cc: antlr-interest at antlr.org
Betreff: Re: AW: [antlr-interest] Use partial classes for C#

Hi,

> Of course, this is how I do it for now. However, partial classes were
> invented for exactly this reason: Mix generated and hand written  
> code in one
> class but in different files. ASP.NET / WinForms etc. do it and a  
> whole
> bunch of other generators do it as well. This is the standard way of
> generating code in the .NET world.
>
> So I think ANTLR should follow this.

I really don't care how ANTLR does this in .NET, but just out of  
interest: what is the supposed benefit of this feature if I can also  
solve it using abstract base classes? I don't understand why it was  
necessary to add this feature.

Regards,
Martin




More information about the antlr-interest mailing list