Important notes on Access specifiers in C#


                         Everything about access specifiers.



To use any class can be public, private etc. we should be using corresponding namespace. Everything we talk about here is in assumption that Namespace is already used and dll is already added to the program.         

Public class:


There is no limit for Public class on where it can be accessed.

Within the assembly if you are accessing in the different namespace, namespace of the public class should be added.

If accessing outside the assembly, dll or assembly of class should be added to
the project and namespace also should be added.


Internal class :

Internal Class can be accessed only with in the assembly.
[*Namespace should be used if working in different namespace*]

Below are some important Questions


1.    What is default specifier for Class?
 
ANS: Internal
      
2.   What is default specifier for Method?
        A: Private is the default access specifier.
      
3.   What is default specifier for Interface?
        A: Default Access specifier is public for members in Interface.
      
4.  Can an Interface have private members?
        A: Interface can not have private Members

5.  What is default specifier for Constructor?
       A: Public is the default access specifier for constructor.
      
6.  What if we create a private constructor?
        A: Private constructs blocks us from creating an object.protection-level issues comes up when trying to create an object.
      
7.  Can we have a private class?
        A: Yes .we can have a private class .
        But a class directly under namespace can be only Public or Internal.

8.  What is the necessity of nested class?[Out of scope of this article.]
      
9.  Can a derived class have a bigger scope than its base class?

        Ans: No.Base class should always have higher access scope.

10. Can we call Base class protected method from the derived class with base class object?
         Ans: Base class protected Method cannot be called with the even with the base class  object In the derived class.

        Protected Method or field can be called only with the derived class object in the derived Class.




Post a Comment

0 Comments