Thursday, February 10, 2011

Understanding Access Modifiers/Specifiers in C#


Access modifiers are keywords (private, public, internal, protected and protected internal) which are used to specify the accessibility of a type and its members.

public class AccessModifier
{
    // Available only to the container Class
    private string privateVariable;

    // Available in entire assembly across the classes
    internal string internalVariable;

    // Available in the container class and the derived class
    protected string protectedVariable;

    // Available to the container class, entire assembly and to outside
    public string publicVariable;

    // Available to the derived class and entire assembly as well
    protected internal string protectedInternalVariable;

    private string PrivateFunction()
   {
        return privateVariable;
   }

    internal string InternalFunction()
   {
        return internalVariable;
   }

    protected string ProtectedFunction()
   {
        return protectedVariable;
   }

   public string PublicFunction()
   {
        return publicVariable;
   }

   protected internal string ProtectedInternalFunction()
   {
        return protectedInternalVariable;
   }
}

Now to demonstrate the behaviour how these class members are exposed to another class depending upon their scope defined via modifier/specifier and when we create an object or inherit from the above created class named "AccessModifier"
As mentioned in the above shown code and before each member variable I wrote a comment which describes the access level of each type of member varaible.

So if we derive another class "CallAccessModifier" from the parent class "AcessModifier" then "private" type members will not be visible because its considered as outside the scope of parent class.

But "protected" members are the ones which become available only during the inheritance (when a child is derived from a parent class) as shown via image below.

Showing class members availability through inheritance.




















                         
Note:-
"this" is a keyword referes to the current instance of the class and used to access members. In the image above this keyword shows all the available members with the classs.

If you notice in the above shown image it clearly shows all the protected members are visible due to virtue of inheritance, along with other members with scope defined as internal, public and protected internal.

Now let's see what happens if we decide to create an object of the class "AccessModifier" shown in the code above. As per the rule private and protected must not be visible via an object:
Showing members available when an object is created of the given class
















                     
        
Now as shown in the image just above we are not able to see private and protected members because they both are not exposable via an object.

Besides we are able to see protected internal member, and this is correct because its a mix of both protected and internal and so exposed in here because we are in the same assembly.

Q. What happens if we build a .dll of the "AccessModifier" code and use it in other project.
A. The rules remain the same, once AccessModifier.dll is refered in other project its considered as outside ot the current assembly and so:
 ** private members are not exposed
 ** internal members are not exposed

Tuesday, January 11, 2011

Windows Azure platform 30 day pass
















 


Click on the URL http://www.windowsazurepass.com/?campid=9FE3DB53-E4F0-DF11-B2EA-001F29C6FB82 and use promo code MPR001. No credit card required. With the Windows Azure platform you pay only for what you use, scale up when you need capacity, and pull back when you don't. Plus, get no-cost technical support with Microsoft Platform Ready.

Thursday, September 09, 2010

Existence of Third-Party ADO.NET Data Providers

Besides Microsoft SQL Server there are various other databases which exist in industry and are widely used among the various corporate clients. While you will most likely be able to obtain an ADO.NET data provider directly from the database but you can always go and get the ADO.NET data provider for the database of your choice from www.sqlsummit.com/DataProv.htm

The ADO.NET providers exist for following dbs SQLite, IBM DB2, MySQL, PostgreSQL, TurboDB, Sybase and etc.

Wednesday, September 08, 2010

System.Data.OracleClient.dll with .NET Framework 4.0

If you recall starting .NET 1.1 until .NET 3.5 all the .NET platform shipped with an assembly named System.Data.OracleClient.dll, which was one of the recommended data provider to connect .NET appplication with Oracle databas.seas the name suggests, offered a data provider to communicate with Oracle databases.

With the release of 4.0, Microsoft made this assembly obsolete and will eventually be deprecated.

The reason is certainly not that Microsoft only wants SQL Server and other MS specific Dbs to be used using .NET, but the actual reason is that Oracle corp provides its own custom .NET assembly, which follows the same overall design guidelines as the data providers provided by Microsoft.



You can download this assembly free of cost from Oracle corp's web site at
www.oracle.com/technology/tech/windows/odpnet/index.html.

Tuesday, September 07, 2010

What are the contents of Service1.asmx

In any version of Visual Studio, when you create a WebService, it comes with a default Service1.asmx file. When you double-click on this file it opens Service1.asmx.cs file for you, why? and what is the content in this file then? and how do you view it?

In order to view what is inside Service1.asmx, select the file in Solution-Explorer, right-click, and choose "Open With..."

From the shown dialog, select the option "Source Code(Text) Editor, click OK, you will see content like this:


These are the instructions Service1.asmx have and so it opens the Service1.asmx.cs, whenever you double-click on Service1.asmx, as its .cs file is the CodeBehind.

Monday, September 06, 2010

Where is Web Service Project Template in VS 2010

If you are looking for Web Service project template in Visual Studio 2010 under .NET Framework 4.0 as shown in the image below, you will not find it.


In order to create a WebService using Visual Studio 2010 you need to switch back to .NET Framework 3.5, as shown in the image below, and you will be able to crate a WebServeice project.