C# App Domain

Introduction

 
In this blog, we are discussing the importance of the app domain.
 
What is the App domain? 
 
App domain is a logically isolated container inside which .Net code runs. Let's understand the app domain concepts with the help of a simple program. When I run this program, what happens internally?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace AppDomain  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             A aobj = new A();  
  14.             B bobj = new B();  
  15.             Console.Read();  
  16.         }  
  17.     }  
  18.   
  19.     public class A  
  20.     {  
  21.   
  22.     }  
  23.   
  24.     public class B  
  25.     {  
  26.   
  27.     }  
  28. }  
This small console application is running as a process inside the operating system. Inside this process, we have the app domain by default loaded, in that app domain, both of the objects are running. In other words, by default, there is always an app domain under which your code runs. 
 
We have understood the app domain with the help of the above program. Now, we will learn the importance of the app domain.
Let's assume we are using one third party class, and we really don't know what exactly third party DLL does. In such a case, for security reasons, we can run third party DLL in a secured app domain. Please check the below code for more understanding.
  1. AppDomain domain = AppDomain.CreateDomain("SecuredAppDomain");  
  2. Type t = typeof(ThirdpartyClassName);  
  3. domain.CreateInstanceAndUnwrap(t.Assembly.FullName,t.FullName);  

Summary

 
In this blog, we discussed the importance of app domain.
Next Recommended Reading Simple Calculator Console App Using C#