Thursday 13 August 2015

C# Interview Q&A

1) What is Access modifiers in C# ?

Access modifiers are  used to specify the scope of accessibility of a member of a class or type of the class itself. For example, a public class is accessible to everyone without any restrictions, while an internal class may be accessible to the containing assembly only.

Types : 

Private: limits the accessibility of a member to within the defined type, for example if a variable or a functions is being created in a ClassA and declared as private then another ClassB can't access that.

Public: has no limits, any members or types defined as public can be accessed within the class, assembly even outside the assembly. Most DLLs are known to be produced by public class and members written in a .cs file.

Internal: internal plays an important role when you want your class members to be accessible within the assembly. An assembly is the produced .dll or .exe from your .NET Language code (C#). Hence, if you have a C# project that has ClassA, ClassB and ClassC then any internal type and members will become accessible across the classes with in the assembly.

Protected:
plays a role only when inheritance is used. In other words any protected type or member becomes accessible when a child is inherited by the parent. In other cases (when no inheritance) protected members and types are not visible.

Protected internal:
is a combination of protected and internal both. A protected internal will be accessible within the assembly due to its internal flavor and also via inheritance due to its protected flavor.

Code :

1) Lets create a console c# application.
2) Create a new class file and add the code as below ,

class Class1
    {
        //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()
        {
            privateVariable = "";
            return privateVariable;
        }

        internal string InternalFunction()
        {
            privateVariable = "";
            internalVariable = "";
            return internalVariable;
        }

        protected string ProtectedFunction()
        {
            privateVariable = "";
            internalVariable = "";
            return protectedVariable;
        }

        public string PublicFunction()
        {
            privateVariable = "";
            internalVariable = "";
            return publicVariable;
        }

        protected internal string ProtectedInternalFunction()
        {
            privateVariable = "";
            internalVariable = "";
            return protectedInternalVariable;
        }
    }

3) Go to your program.cs file, In main method create object for your class and try to access the created fields with the object. 

You can see from below image only Internal, Protected Internal and public variable fields can be accessible.

Private is not shown due to its limited scope with in the class.

Protected members also not showing in below, because protected members can be accessed with derived class inheriting parent class.
 



Create a new Console Application project and create a class file, create a method in that class. Add reference of our above project to this project. Create a instance of Class1 of our previous project in this project method , You can see only public fields and methods can be accessible, if our above project class is private it cannot be accessed in this project. By default the access modifier of class is Internal  and private for class members.



Inherit from Class1 of that project to this project class as below image,



You can see in test1 method, we can able to see protected , protected Internal and public methods and fields with this keyword after inheriting from parent cclass in that project.

Internal will be accessed only with in assembly meaning with that  project dll or exe.



No comments:

Post a Comment