Saturday, 31 August 2019

Difference among var, dynamic and object

Knowing var, dynamic and object
object
Everything in .net framework is inherited from object class. This is a base type. Every datatype can be boxed in the object type
Example
object a = 10; // int
object b = new Customer(); // customer object
object c = new Product(); // product object
object d = "Jon"; // string
object e = new { Name = "Felipe", Age = 20 }; // anonymous type

var
var is a keyword in c#. It allows you to define a variable which can contain any type. Once it is assigned a type the variable can contain only that type
Example
var a = 10; // int
var b = 10d; // double
var c = "text"; // string
var p = new Product(); // Product type

Here if we try to use the ‘a’ again and assign string data to it, it will now allow.
dynamic
As the name suggest dynamic is a special type which does not compile during normal compilation period. It is used when the output or the input type is unknown, also you can reuse the same variable and assign any type to it during the same method run
Example
dynamic cust = new CustomerDetails();
cust.Age = 18;
cust.Name = "Ishoo";
cust.Name; // string
cust.Age; // int


To learn more on dynamic and var download the project from gihub and look for ‘Accessing and setting private or internal of different project using reflection’ region in Main of program.cs

Saturday, 24 August 2019

Difference between IEnumerable and IEnumerator

This is asked very frequently in any .net framework interview

Things that should be noted-

Here is the documentation on Enumerator.
IEnumerator is used to go through list
IEnumerator has one object type property i.e Current which has getter only and two methods
         MoveNext that returns boolean
         Reset

Code sample

IEnumerator enumerator = enumerable.GetEnumerator();

while (enumerator.MoveNext())
{
    object element = enumerator.Current;
    // Perform your logic logic on the element
}
Let's consider a scenario where you have to loop through a collection in two different methods

take the example above
List<int> enumerable = new List<int>();
for (int i = 0; i <= 20; i++)
{
   enumerable.Add(i);
}

IEnumerator enumerator = enumerable.GetEnumerator();

Console.WriteLine("FirstLoopMethod start...");
while (enumerator.MoveNext())
{
    object element = enumerator.Current;
    int num = int(element);
    Console.WriteLine(num);
    if(num > 100)
    {
       SecondLoopMethod(enumerator);
    }
// you logic here // Perform your logic logic on the element }
private void SecondLoopMethod(IEnumerator enumerator)
{
    Console.WriteLine();
    Console.WriteLine("SecondLoopMethod start...");
    while (enumerator.MoveNext())
    {
       object element = enumerator.Current;
      int num = int(element);
      Console.WriteLine(num);
      // Perform your logic logic on the element
   }
}
This is the output you get


Here you can see in the SecondLoopMethod the iteration started from where we left it in the first method

That's the benefit and control of using IEnumerator

Where if we try to this with IEnumerable then in the second method it again start looping from the beginning

Documetation for Enumerable.
We can loop throgh items of list using IEnumerator too
IEnumerable and IEnumerator are both interfaces in the .net framework
IEnumerable has just one method declaration i.e. GetEnumerator which return an enumerator
Every object which can be looped through using foreach, inherit from IEnumerable directly or indirectly
You cannot use foreach on classes which do not inherit from IEnumerable
If you have a model and want to loop through its array using foreach then you must that class must inherit from IEnumerable

Code sample

foreach(object item in enumerable)
{
    // Perform logic on the item
}
When your code executes the above code becomes

IEnumerator enumerator = enumerable.GetEnumerator();

while (enumerator.MoveNext())
{
    object item = enumerator.Current;
    // Perform logic on the item
}
Now what are the benefits and drawbacks of both 
IEnumerable simplifies and shortens the original looping syntax compared to IEnumerator

  1. IEnumerator provide better flexibility and control over your looping logic

Send All Azure App Insights Or Logs Data To Tool Like Datadog

  Introduction Microsoft Azure provides a variety of insights and monitoring logs that you can monitor to check the state of a resource and ...