Monday, November 14, 2022

Differentiate Dependency Inversion vs dependency Injection in c#

 The Inversion of Control is a fundamental principle used by frameworks to invert the responsibilities of flow control in an application, while Dependency Injection is the pattern used to provide dependencies to an application's class.


https://www.c-sharpcorner.com/UploadFile/cda5ba/dependency-injection-di-and-inversion-of-control-ioc/

Differences Between Task And Thread in c#

 Differences Between Task And Thread :

  • Task is more abstract then threads. It is always advised to use tasks instead of thread as it is created on the thread pool which has already system created threads to improve the performance.
  • The task can return a result. There is no direct mechanism to return the result from a thread.
  • Task supports cancellation through the use of cancellation tokens. But Thread doesn't.
  • A task can have multiple processes happening at the same time. Threads can only have one task running at a time.
  • You can attach task to the parent task, thus you can decide whether the parent or the child will exist first.
  • While using thread if we get the exception in the long running method it is not possible to catch the exception in the parent function but the same can be easily caught if we are using tasks.
  • You can easily build chains of tasks. You can specify when a task should start after the previous task and you can specify if there should be a synchronization context switch. That gives you the great opportunity to run a long running task in background and after that a UI refreshing task on the UI thread.

  • A task is by default a background task. You cannot have a foreground task. On the other hand a thread can be background or foreground.

  • The default TaskScheduler will use thread pooling, so some Tasks may not start until other pending Tasks have completed. If you use Thread directly, every use will start a new Thread.


Why we need Tasks
 
It can be used whenever you want to execute something in parallel. Asynchronous implementation is easy in a task, using’ async’ and ‘await’ keywords.

Why we need a Thread
 
When the time comes when the application is required to perform few tasks at the same time.
 
Here is a beginner tutorial on Introduction to Threading in C# 


Try , Catch and finally in C#

  1.  Multiple finally blocks in the same program are not allowed.
  2. The finally block does not contain any return, continue, break statements because it does not allow controls to leave the finally block.
  3. we can use multiple catch block in the same program.
  4. finally always executed in the block.

  5. The main purpose of finally block is to release the system resources.
  6. Can finally block be used without catch ? - Yes.



Wednesday, November 9, 2022

What is Difference between Application variables and session variables in c#

 Application variable: it is used for entire application which common for all users. It is not for user specific. this variable value will lost when application restarted. Session variable :- this variable is for user specific. the value of the session is available till the user is logged in. 


Application variable create and maintained value in global.asax file.

Friday, November 4, 2022

Thursday, November 3, 2022

What is Lazy Loading and eager loading in c#

 lazy loading delays the initialization of a resource, eager loading initializes or loads a resource as soon as the code is executed.




Eager Loading When you are sure that want to get multiple entities at a time, for example you have to show user, and user details at the same page, then you should go with eager loading. Eager loading makes single hit on database and load the related entities.


Lazy loading When you have to show users only at the page, and by clicking on users you need to show user details then you need to go with lazy loading. Lazy loading make multiple hits, to get load the related entities when you bind/iterate related entities.