locked
public, partial, and private keywords description RRS feed

  • Question

  • I want to know the explanation of the keywords private, public  , partial . And how to use it in the Inheritance .
    i am don
    Friday, July 15, 2011 3:38 PM

Answers

  • Read on MSDN article suggested by Tim. There are lots of them that discuss on these keywords. And there are many more articles in the internet which give lot of information.

    I will give a start up to help you get a general idea.

    In C# - there are 4 access specifiers (specifiers which limit/restrict the access of the class/methods/properties/variables etc).

    They carry the common sense meaning within their name and they are as follows:

    1) Public - Accessible to all (as in public parks) - In c# it implies to accessibility by any other object.

    2) Private - Accessible to someone or something in particular (like your private property is yours or your family's) - In c# it implies to accessibility by object that are within the scope/region of the current objectwhich has this access specifier)

    3) Protected - Accessible to something to which it belongs and a specific set of people (such as National Forests which have the signs - Protected - indicating its for a set of people and the wildlife there) - In c# it implies to accessibility by the class containing the object and the inheriting sub-classes.

    4) Internal - Accessible internally (such as a company's cafeteria for its employees.) - In c# it implies to accessibility within a particular assembly (a dll in simple sense).

    Now, coming to your keyword Partial. Partial -like its dictionary meaning implies- Partial i.e. not complete or full. Partial classes are classes whose implementation is not confined to a particular file/region. They can be distrubuted over different files (within the same namespace I believe- but better you actually check out on its scope). The class is broken into piece for the common reason of it being too long. One common area where you see it without knowing is - THE ASP.NET WEB FORMS! The asp code that you write(source code) and the code behind(c# or vb etc) are partial classes - which are ultimately compiled as one single class.

    Hope this helps. If you have some questions confined to a limited scope, it would be easier to answer - rather than go for a general wider topic (unless its to just get the top beginner information)

    • Proposed as answer by Royal Arun Prakash Saturday, July 16, 2011 12:00 PM
    • Marked as answer by Lie You Friday, July 22, 2011 3:47 AM
    Friday, July 15, 2011 6:59 PM
  • You can find the best answer in Microsoft's on documentation: http://msdn.microsoft.com/en-us/library/ms173121.aspx

    Basically these modify how other classes can interact with your class. Private members are invisible to everyone outside your class, public members can be viewed (and called or changed) by anyone. Internal and protected are two others which you didn't ask about, but are still very common and worth reading up on.

    Partial is completely unrelated to the others. The first 4 are visibility modifiers. Partial means that this one class is defined in multiple different files. When the project is built, the files will be combined into one class. This is really common when working with the studio. If you create a new form, some of the code for that form is generated automatically by the studio and stored in one file. Your own code goes into a completely different .cs file, and the two are combined during the build. This is why, when you look at the code for your Form in Visual Studio, you don't see where all of your controls are declared.


    Check out My Blog for tech news, development tips, and other information for geeks like me.
    • Proposed as answer by Thuru VMVP Friday, July 15, 2011 5:55 PM
    • Marked as answer by Lie You Friday, July 22, 2011 3:48 AM
    Friday, July 15, 2011 3:50 PM

All replies

  • You can find the best answer in Microsoft's on documentation: http://msdn.microsoft.com/en-us/library/ms173121.aspx

    Basically these modify how other classes can interact with your class. Private members are invisible to everyone outside your class, public members can be viewed (and called or changed) by anyone. Internal and protected are two others which you didn't ask about, but are still very common and worth reading up on.

    Partial is completely unrelated to the others. The first 4 are visibility modifiers. Partial means that this one class is defined in multiple different files. When the project is built, the files will be combined into one class. This is really common when working with the studio. If you create a new form, some of the code for that form is generated automatically by the studio and stored in one file. Your own code goes into a completely different .cs file, and the two are combined during the build. This is why, when you look at the code for your Form in Visual Studio, you don't see where all of your controls are declared.


    Check out My Blog for tech news, development tips, and other information for geeks like me.
    • Proposed as answer by Thuru VMVP Friday, July 15, 2011 5:55 PM
    • Marked as answer by Lie You Friday, July 22, 2011 3:48 AM
    Friday, July 15, 2011 3:50 PM
  • Read on MSDN article suggested by Tim. There are lots of them that discuss on these keywords. And there are many more articles in the internet which give lot of information.

    I will give a start up to help you get a general idea.

    In C# - there are 4 access specifiers (specifiers which limit/restrict the access of the class/methods/properties/variables etc).

    They carry the common sense meaning within their name and they are as follows:

    1) Public - Accessible to all (as in public parks) - In c# it implies to accessibility by any other object.

    2) Private - Accessible to someone or something in particular (like your private property is yours or your family's) - In c# it implies to accessibility by object that are within the scope/region of the current objectwhich has this access specifier)

    3) Protected - Accessible to something to which it belongs and a specific set of people (such as National Forests which have the signs - Protected - indicating its for a set of people and the wildlife there) - In c# it implies to accessibility by the class containing the object and the inheriting sub-classes.

    4) Internal - Accessible internally (such as a company's cafeteria for its employees.) - In c# it implies to accessibility within a particular assembly (a dll in simple sense).

    Now, coming to your keyword Partial. Partial -like its dictionary meaning implies- Partial i.e. not complete or full. Partial classes are classes whose implementation is not confined to a particular file/region. They can be distrubuted over different files (within the same namespace I believe- but better you actually check out on its scope). The class is broken into piece for the common reason of it being too long. One common area where you see it without knowing is - THE ASP.NET WEB FORMS! The asp code that you write(source code) and the code behind(c# or vb etc) are partial classes - which are ultimately compiled as one single class.

    Hope this helps. If you have some questions confined to a limited scope, it would be easier to answer - rather than go for a general wider topic (unless its to just get the top beginner information)

    • Proposed as answer by Royal Arun Prakash Saturday, July 16, 2011 12:00 PM
    • Marked as answer by Lie You Friday, July 22, 2011 3:47 AM
    Friday, July 15, 2011 6:59 PM