Pages

Labels

Thursday, February 7, 2013

C# Access Specifiers


C# Access Specifiers

These are used to control the visibility of the class members from outside the world.
They are used to set the scope of the members.
Access Specifiers can be applied for variables, properties and methods(functions)

Types of Access Specifiers

  1. private
  2. protected
  3. internal
  4. protected internal
  5. public

Scope of the Access Specifiers

Access Specifiers
Same namespace(Assembly)

OutSide the namespace(Assembly)
Same Class
Sub Class
Non Sub Class
Sub Class
Non Sub Class
private
ü  
X
X
X
X
protected
ü  
ü  
X
ü  
X
internal
ü  
ü  
ü  
X
X
protected internal
ü  
ü  
ü  
ü  
X
public
ü  
ü  
ü  
ü  
ü  

Default access Specifiers

If no access specifiers are mentioned for a class, variable, property or methods, C# itself assigns access specifiers for these types.

  1. A class has a default access  specifier as internal. A class defined directly in a namespace cannot be explicitly declared as private, protected, or protected internal.
  2. An enum has a default access specifier as public.
  3. An interface has a default access specfier as public.
  4. A struct has a default access  specifier as internal.
  5. A method, field, and property has default access specifier as private.

Syntax:


namespace [name_of_the_namespace]
{
    [access_specifier] class [name_of_the_class]
    {
        //Variable declaration
        [access_specifier] [datatype1] [variable_name1],[variable_name2],[...];
        [access_specifier] [datatype2] [variable_name3],[variable_name4],[...];

        //method declaration
        [access_specifier] [return_type] [name_of_the_function1]([parameters])
        {
        }

        //method declaration
        [access_specifier] [return_type] [name_of_the_function2]([parameters])
        {
        }       
    } 
}


6 comments:

  1. A method of financing a van instead of buying
    it outright. The issue which comes up is not knowing about the different van
    or car leasing which is available and not getting to use the vehicle because of the cost associated with the monthly payment.
    Vehicles are rented on a daily, weekend or weekly rate for
    a short period - typically not more than a few weeks.

    Here is my homepage Van Leasing

    ReplyDelete
  2. The growth of bacteria is inhibited at these high levels of sugar.
    It is for this reason that you should use cranberries along with
    any other yeast infection treatments. There are 3 main
    symptoms of a yeast infection, the first is itchiness in the
    vulva and surrounding areas, it can become so unbearable at times.


    my weblog - natural cure for yeast infection

    ReplyDelete
  3. Stretch marks are formed when fibroblasts are somehow restricted and skin vitality is
    not ok. And also some of the underweight people are also such complaints.
    There may be a new and innovative cellulite treatment available now:
    cryotherapy.

    Also visit my web site Get rid of cellulite

    ReplyDelete
  4. But cellulite can also be a problem for some men, and with
    certain hormonal conditions it is actually quite common.
    It contains natural ingredients like olive oil,
    jojoba oil, and other essential oils. You must understand that the
    body is designed to move.

    Also visit my homepage - Stretch mark removal

    ReplyDelete
  5. There are several service providers that are not genuine.
    I had a different Youtube account that I was using previously but the reason I created
    the new one was because Google and Youtube joined together and I always kept my gmail opened and was unable to
    be signed into my gmail and my Youtube account (which used
    a yahoo email address) at the same time. Also, economic
    growth is unlimited in the world and should not be restricted by worry
    about natural resources.

    Stop by my web blog ... increase youtube views and ratings

    ReplyDelete