C#
  Home arrow C# arrow Page 6 - Defining Member and Type Visibility
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Defining Member and Type Visibility
By: Ayad Boudiab
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-04-16

    Table of Contents:
  • Defining Member and Type Visibility
  • Public Members
  • Private Members
  • Protected Members
  • Internal and Protected Internal Members
  • Type Visibility

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Defining Member and Type Visibility - Type Visibility


    (Page 6 of 6 )

    Public Type

    Creating a public type means that the type is accessed from other types in the current assembly as well as other assemblies. This is useful when you are creating a code library. Here is an example:


    public class Book

    {

    private string title;

    private string author;

    private string ISBN;

    }


    Since the Book class is public, it can be accessed from other assemblies. The users of this assembly can create Book objects, inherit from the Book class, and so on…

    Internal Type

    Internal types, on the other hand, can be used only by the assembly in which they are defined. So, if we make the Book class internal, other assemblies cannot in any way interact with the Book class:


     internal class Book

    {

    private string title;

    private string author;

    private string ISBN;

    }


    Please note that the default access modifier for a type is internal. In other words, if you do not specify the access modifier, it will be internal. So the following declarations are identical:


     class Book {...}


    internal class Book {...}


    Conclusion 

    When creating types, it is very important to pay special attention to the type member’s visibility. Declaring a member private makes it accessible only to the defining class. Declaring the member public, on the other hand, makes it accessible by any type. When it comes to inheritance, try to keep members protected so that only subclasses can access them. As far as building your own libraries, internal and protected internal members come handy.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hello everyone,Welcome to my first article of the series of introduction to C# and...
     

    C# ARTICLES

    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT