Thursday, 11 July 2013

Compares the access modifiers in VB.NET |What are class access modifiers? | What are class access modifiers in .NET

                               Hi friends ,in this article I will explain about how to Compares the access modifiers in VB.NET |What are class access modifiers? | What are class access modifiers in .NET 
What are class access modifiers?
Access modifiers are keywords used to specify the declared accessibility of a member or a type. This section introduces the four access modifiers:
  •  Public
  • Protected
  • Private
  • Friend ( Internal in C#) and
  • Protected friend (protected internal in C#)

 Public:
The public keyword in the declaration statement specifies that the elements are accessible from code anywhere within the same project, from other projects that reference the project, and from any assembly built from the project. The following code shows a sample Public declaration.

Public Class Aspdotnetroja


You can use Public only at module, interface, or namespace level. This means you can declare a public element at the level of a source file or namespace, or inside an interface, module, class, or structure, but not within a procedure.

Protected:
Protected means that elements are accessible only from within their own class or from a derived class. Protected can also be used with the Friend keyword. When they're used together, elements are accessible from the same assembly, from their own class, and from any derived classes. The following code shows a sample Protected declaration.

Protected Class AutolispKishore


Private:
Private access modifier is applicable only to the members of a type. It restricts access to the members within the type itself. Here, the member variable strTitle is declared as private inside a class Book. Hence, it cannot be accessed from outside the class Book. Remember that private access modifier is applicable only to type members not to the type itself. This means you cannot declare a class as private but if your class is a nested then it can be declared as private. The following code shows a sample Private declaration.

Private i As Integer


Friend:
Friend access means that elements are accessible only within the program. Friend is the default access modifer for any class that does not have a modifier. The following code shows a sample Private declaration.

Friend str As String


Protected Friend Access:
Protected Friend access modifier is a combination of protected and friend access modifiers and allows access to class members in the same project and all the inherited types. The following code shows a sample Private declaration.

Protected Friend str As String


The following table compares the access modifiers.

Access modifier
Access level granted
Elements you can declare with this access level
Declaration context within which you can use this modifier
Public
Unrestricted:
Any code that can see a public element can access it
Interfaces
Modules
Classes
Structures
Structure members
Procedures
Properties
Member variables
Constants
Enumerations
Events
External declarations
Delegates
Source file
Namespace
Interface
Module
Class
Structure
Protected
Derivational:
Code within the class that declares a protected element, or a class derived from it, can access the element
Interfaces
Classes
Structures
Procedures
Properties
Member variables
Constants
Enumerations
Events
External declarations
Delegates
Class
Friend
Assembly:
Code within the assembly that declares a friend element can access it
Interfaces
Modules
Classes
Structures
Structure members
Procedures
Properties
Member variables
Constants
Enumerations
Events
External declarations
Delegates
Source file
Namespace
Interface
Module
Class
Structure
ProtectedFriend
Union of Protected and Friend:
Code within the same class or the same assembly as a protected friend element, or within any class derived from the element's class, can access it
Interfaces
Classes
Structures
Procedures
Properties
Member variables
Constants
Enumerations
Events
External declarations
Delegates
Class
Private
Declaration context:
Code within the type that declares a private element, including code within contained types, can access the element
Interfaces
Classes
Structures
Structure members
Procedures
Properties
Member variables
Constants
Enumerations
Events
External declarations
Delegates
Module
Class
Structure

Thanks for reading my blog Aspdotnet-Roja.if you like my blog why are you waiting like us on facebook page AspdotnetRoja.

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.