C#
  Home arrow C# arrow 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


    (Page 1 of 6 )

    This article provides an in-depth discussion of member and type visibility. It illustrates when and where to make a member either public or private, protected or internal. It even gives you tips to use when building your own libraries. Keep reading to find out more!

    Before we get started, let’s clarify some terms. Types are the building blocks of an object-oriented language, like C#. An application is a collection of these types interacting together to provide useful information. When we refer to types, we mean classes, structures, interfaces, enumerations, and delegates (these types will be explored in more detail in upcoming articles).

    A type, like a class, contains members. Example of members will be methods, fields, constructors, and so on…

    As stated earlier, an object-oriented application is a collection of types, and a type is a collection of members. When designing a type, a common question arises: should the user of the class (another developer for instance) have access to a specific field in the class, or should that field be privately owned and manipulated by the type? In other words, what is the visibility of each member of the class? The reason this is important is that you do not want the user of your class to change the state of a certain field to an unacceptable value and thus, make that instance of the class useless.

    Now that we've clarified the importance of member visibility, here is a summary of those visibilities before we explore them in more detail:


    public

    This marks the member as accessible from other classes.

    private

    This marks the member as accessible from only the class that defined the member. In C#, all members are private by default.

    protected

    This marks the member as accessible from the defining class, and any derived classes.

    internal

    This marks the member as accessible from other types in the same assembly.

    protected internal

    This marks the member as accessible from the current assembly or from types derived from the defining class in the current assembly.


    Note: C# uses the term ‘access modifier’ to describe the accessibility.

    More C# Articles
    More By Ayad Boudiab


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

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - 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#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek