C# Interview Questions on Access Modifiers




What are Access Modifiers in C#?

In C# there are 5 different types of Access Modifiers.

Public

The public type or member can be accessed by any other code in the same assembly or another assembly that references it.



Private

The type or member can only be accessed by code in the same class or struct.



Protected

The type or member can only be accessed by code in the same class or struct, or in a derived class.



Internal

The type or member can be accessed by any code in the same assembly, but not from another assembly.



Protected Internal

The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.



What are Access Modifiers used for?

Access Modifiers are used to control the accessibilty of types and members with in the types.





Can you use all access modifiers for all types?

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.



Can derived classes have greater accessibility than their base types?

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.





Is the following code legal?



using System;

private class Test

{

   public static void Main()

   {

   }

}



No, a compile time error will be generated stating "Namespace elements cannot be explicitly declared as private, protected, or protected internal"



Can you declare struct members as protected?

No, struct members cannot be declared protected. This is because structs do not support inheritance.



Can the accessibility of a type member be greater than the accessibility of its containing type?

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.



Can destructors have access modifiers?

No, destructors cannot have access modifiers.



What does protected internal access modifier mean?

The protected internal access means protected OR internal, not protected AND internal. In simple terms, a protected internal member is accessible from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected.



What is the default access modifier for a class,struct and an interface declared directly with a namespace?

internal



Will the following code compile?



using System;

interface IExampleInterface

{

   public void Save();

}



No, you cannot specify access modifer for an interface member. Interface members are always public.



Can you specify an access modifier for an enumeration?

Enumeration members are always public, and no access modifiers can be specified.

--------------

Why should you override the ToString() method

Why should you override the ToString() method?

All types in .Net inherit from system.object directly or indirectly. Because of this inheritance, every type in .Net inherit the ToString() method from System.Object class. Consider the example below.



using System;

public class MainClass

{

  public static void Main()

  {

   int Number = 10;

   Console.WriteLine(Number.ToString());

  }

}



In the above example Number.ToString() method will correctly give the string representaion of int 10, when you call the ToString() method.



If you have a Customer class as shown in the below example and when you call the ToString() method the output doesnot make any sense. Hence you have to override the ToString() method, that is inherited from the System.Object class.



using System;

public class Customer

{

 public string FirstName;

 public string LastName;

}

public class MainClass

{

 public static void Main()

 {

  Customer C = new Customer();

  C.FirstName = "David";

  C.LastName = "Boon";

  Console.WriteLine(C.ToString());

 }

}







The code sample below shows how to override the ToString() method in a class, that would give the output you want.





using System;

public class Customer

{

  public string FirstName;

  public string LastName;



  public override string ToString()

  {

    return LastName + ", " + FirstName;

  }

}

public class MainClass

{

  public static void Main()

  {

    Customer C = new Customer();

    C.FirstName = "David";

    C.LastName = "Boon";

    Console.WriteLine(C.ToString());

  }

}



Conclusion : If you have a class or a struct, make sure you override the inherited ToString() method.

No comments:

Post a Comment