Friday, May 23, 2014

Acess Specifiers of C#.Net Interview Questions(11)


Acess Specifiers of C#.Net Interview Questions(11)



1.How many types of access specifiers are there in c#.net?
A)There are 5 types of access specifiers are there in C#.Net.They are:
            1.Public
            2.Protected
            3.Internal
            4.Protected Internal
            5.Private
2.Brief about Access specifiers in C#.Net?
A) The following five accessibility levels can be specified using the access modifiers:
Public : Access is not restricted.
Protected : Access is limited to the containing class or types derived from the containing class.
Internal : Access is limited to the current assembly.
Protected internal: Access is limited to the current assembly or types derived from the containing class.
Private : Access is limited to the containing type.

3.What is the purpose of access specifiers?
A)Access  are used to provides restricted accessibility of types and members with in their declaration.

4.Can you use all access modifiers for all types?
A)No, Not all access modifiers can be used by all types or members in all contexts, and in some cases the accessibility of a type member is constrained by the accessibility of its containing type.

5.Can derived classes have greater accessibility than their base types?
A)No, Derived classes cannot have greater accessibility than their base types. For example the following code is illegal.
using System;
internal class InternalBaseClass
{
    public void Print()
    {
      Console.WriteLine("I am a Base Class Method");
    }
}
public class PublicDerivedClass : InternalBaseClass
{
    public static void Main()
    {
      Console.WriteLine("I am a Public Derived Class Method");
    }
}

When you compile the above code an error will be generated stating "Inconsistent accessibility: base class InternalBaseClass is less accessible than class PublicDerivedClass".To make this simple, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.

6.Can you declare struct members as protected?
A)No, struct members cannot be declared protected. This is because structs do not support inheritance.

7.Can the accessibility of a type member be greater than the accessibility of its containing type?
A)No, the accessibility of a type member can never be greater than the accessibility of its containing type. For example, a public method declared in an internal class has only internal accessibility.

8.Can destructors have access modifiers?
A)No, destructors cannot have access modifiers.

9. What is the default access modifier for a class,struct and an interface declared directly with a namespace?
A)Internal

10.Will the following code compile?
A)using System;
interface IExampleInterface
{
   public void Save();
}
No, you cannot specify access modifer for an interface member. Interface members are always public.

11.Can you specify an access modifier for an enumeration?
A)Enumeration members are always public, and no access modifiers can be specified.

0 comments:

Post a Comment