1. Back To Blog

C# Interview questions :- What is an application domain (App domain)?

Application domain is a logical isolated container inside a process. In this logical isolation you can load and run .NET code in an isolated manner. Below are the benefits of application domain: -

  • You can load and unload DLL inside these logical containers with out one container affecting the other. So if there are issues in one application domain you can unload that application domain and the other application domain work with out issues.
  • If you have a third party DLL and from some reason you do not trust that third party code. You can run that DLL in an isolated app domain with less privelges. For example you can say that the DLL cannot access your "c:" drive. And other DLLs which you trust you can run with full privilege in a different app domain.
  • You can run different versions of DLL in every application domain.

 

Application Domain

Below is a simple code in which we have created a new app domain and in that we have loading and running "Test" class

AppDomain domain = AppDomain.CreateDomain("New domain name");
Type t = typeof(Test);
domain.CreateInstanceAndUnwrap(t.Assembly.FullName,t.FullName);


Below is a simple C# code where we are loading and running a class "Test" with less priveleges. The codes from class "Test" do not have any access to the "c:" of my computer. 

var perm = new PermissionSet(PermissionState.None);
perm.AddPermission(
new SecurityPermission(
SecurityPermissionFlag.Execution));
perm.AddPermission(
new FileIOPermission(FileIOPermissionAccess.NoAccess, @"c:"));
var setup = new AppDomainSetup();
setup.ApplicationBase =AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
AppDomain domain = AppDomain.CreateDomain("New domain name", null, setup, perm);
Type t = typeof(Test);
domain.CreateInstanceAndUnwrap(
t.Assembly.FullName,
t.FullName);

Do not forget to watch below C#(CSharp) interview questions and answers series :-

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social