locked
Partial Classes RRS feed

  • Question

  • I understand that partial classes are intended to allow global access to a given class. So for example if I declare a partial class called Contact() in Class.cs and then try to create a new Contact() in Program.cs it'll be available.

    This seems to work even if I don't declare the class as partial. Can someone explain why this is please?
    Friday, November 2, 2007 8:13 AM

Answers

  • No, the concept of a partial class is entirely unrelated with wether access to it is global. Partial classes were added to version 2 of the C# language in order to allow the definition of a class to be split into more than one source file, which was not allowed in version 1.

     

    This is most useful when you are using some tool that automatically generates code for a class, and then you want to manually add some members of your own to that class. Using partial classes, you can add your own members into a separate source file without worry that they will be overwritten when the generator rewrites the automatic file. One example of a place were this happens is the Windows Forms designer in Visual Studio 2005, which generates code for the Form class in the .designer.cs file, and then provides a separate file for your own code. The dataset designer and the web service proxy class generator are two additional examples.

     

    Partial classes are also useful when you have a very large class, so you can distribute the code into various separate source files. This also allows various developers to work on different parts of the class under a source code contol system.

     

    Friday, November 2, 2007 8:36 AM

All replies

  • Are they under the same namespace, your 'program' and 'Class' classes?
    Friday, November 2, 2007 8:29 AM
  • No, the concept of a partial class is entirely unrelated with wether access to it is global. Partial classes were added to version 2 of the C# language in order to allow the definition of a class to be split into more than one source file, which was not allowed in version 1.

     

    This is most useful when you are using some tool that automatically generates code for a class, and then you want to manually add some members of your own to that class. Using partial classes, you can add your own members into a separate source file without worry that they will be overwritten when the generator rewrites the automatic file. One example of a place were this happens is the Windows Forms designer in Visual Studio 2005, which generates code for the Form class in the .designer.cs file, and then provides a separate file for your own code. The dataset designer and the web service proxy class generator are two additional examples.

     

    Partial classes are also useful when you have a very large class, so you can distribute the code into various separate source files. This also allows various developers to work on different parts of the class under a source code contol system.

     

    Friday, November 2, 2007 8:36 AM
  • Yes, it looks like they are. I added the Class.cs by right clicking on the project and selecting Add > Class and the auto generated code did this.

    I guess what you're pointing out is that partial makes the class available to other files even when the're outside of the namespace?
    Friday, November 2, 2007 8:38 AM
  • Ah! - Excellent answer. Thanks you!
    Friday, November 2, 2007 8:39 AM
  • What do you mean by 'it'll be available'  ?do you mean that you can still create another 'contact' class in Program.cs though you have already defined the same class in Class1.cs ?If yes,you have got Partial class concept all wrong my friend.

    Partial class scope is  you can write some part of a class in one location and extend its functionality with the same signature in another location.That means during compilation both classes will be treated as one only.

    But in your case they are not partial classes neither they are same classes.As they are nested inside different classes,they belong to the class in which they have been defined,not to the whole project.You can access them by object of their container classes only. That is why you are able to define two classes with same name.

    In partial class you cant write same method in both parts.But here in your case,check that you will be able to write same method in both the classes cause they are not one.

    So your scenario and partial class scenario are completely different.

    • Proposed as answer by Saroj k Panda Monday, July 14, 2014 10:14 AM
    Monday, July 14, 2014 10:09 AM