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.

Monday, October 17, 2022

What is the difference between Parse() and TryParse()?

 => int.Parse() will throw an exception

=> int.TryParse() will return false (but not throw an exception)


Parse throws an exception if the conversion from a string to the specified datatype fails, whereas TryParse explicitly avoids throwing an exception.

Example:

int number = int.Parse(textBoxNumber.Text);

// The Try-Parse Method

int.TryParse(textBoxNumber.Text, out number);

Friday, October 14, 2022

What is difference between Var, Object and Dynamic in c#


Object:


Each object in C# is derived from object type, either directly or indirectly. It is compile time variable and require boxing and unboxing for conversion and it makes it slow. You can change value type to reference type and vice versa.


public void CheckObject()

{

    object test = 10;

    test = test + 10;    // Compile time error

    test = "hello";      // No error, Boxing happens here

}


Var:


It is compile time variable and does not require boxing and unboxing. Since Var is a compile time feature, all type checking is done at compile time only. Once Var has been initialized, you can't change type stored in it.


public void CheckVar()

{

    var test = 10;         // after this line test has become of integer type

    test = test + 10;      // No error

    test = "hello";        // Compile time error as test is an integer type

}

Dynamic:


It is run time variable and not require boxing and unboxing. You can assign and value to dynamic and also can change value type stored in same. All errors on dynamic can be discovered at run time only. We can also say that dynamic is a run time object which can hold any type of data.


public void CheckDynamic()

{

    dynamic test = 10;

    test = test + 10;     // No error

    test = "hello";       // No error, neither compile time nor run time

}

Saturday, September 3, 2022

What is difference between Throw and Throw Ex.

The main difference between throw and throw ex in C# is that the throw provides information about from where the exception was thrown and also about the actual exception while the throw ex provides information only about from where the exception was thrown.




Example:

Throw Ex.




Throw :                                                                                                                                     
                                                                                                                                    








Tuesday, August 16, 2022

Sets example in c#

 Sets in the C# refer to the HashSet. It is an unordered collection of unique elements. It refers to the System.Collections.Generic namespace. Mainly it is used when we want to remove the duplicate elements from being inserted in the list. Following is the declaration of the HashSet:


Syntax:

var set = new HashSet<string>(arr1);

To remove the duplicate elements, it is going to be set into an array.


Syntax:

string[] arr2 = set.ToArray();

Difference between List and Set:

S.No.BasisListSet
1.DefineThe List is a type of data structure to store the elements.Sets are also a type of data structure but to stores the unique elements.
2.SequenceA sequence of the elements is important.The sequence doesn’t matter, it only depends upon the implementation.
3.Elements AccessElements in the lists are accessed by using the indices of the elements in the list.In the set, elements are the indices that can be easily accessible.
4.Interface Systems.Collection.IList is the Interface available for the List Implementation.Systems.Collection.ISet is the Interface available for the Set Implementation.
5.Implementation

It can be implemented using two ways:

  • Static List ( using Array )
  • Dynamic List ( using LinkedList )

It can also be implemented using two ways:

  • HashSet ( Hashtable )
  • Sorted Set ( Red Black Tree-based )
6. Duplicity The list can have duplicate elements.Set contains only unique elements.
7. PerformanceList performance is not as good as Set.Sets have good performance than List.
8.Methods 

There are many methods available to apply on the List. Some of them are as :

  • int Add(element)
  • void Insert(int, element)
  • void Clear()
  • int IndexOf(element)

There are many methods available to apply on Set. Some of them are as :

  • bool Add(element)
  • bool Contains(element)
  • bool Remove(element)
  • void Clear()