What is hoisting in JavaScript?

  • In Javascript, Hoisting is a concept in which Javascript brings all the variables declarations to the top of the script during execution.
  • This means that if a variable is initialized at some part of the script while being accessed before its definition is valid and doesn't throw an error.
  • But the assignment of variables happen at their original places which means that although variables are available before their actual place of declaration, their value is undefined.
console.log(`The value of x before is ${x}`);
var x = 10;
console.log(`The value of x after is ${x}`);

output:
The value of x before is undefined
The value of x after is 10
  • variables declared using var keyword in the global execution context are "hoisted", which is why the above code works without issue.
  • let keyword on the other hand by its design doesn't allow this to happen
console.log(`The value of x before is ${x}`);
let x = 10;
console.log(`The value of x after is ${x}`);

output:
console.log(`The value of x before is ${x}`);
                                        ^
ReferenceError: Cannot access 'x' before initialization

What are Anonymous methods and Lambda Expressions in C#?

  • An Anonymous function is an inline block of statements which can be used wherever a delegate type is expected.
  • Anonymous functions can be created by using an anonymous method or a lambda expression.
  • An Anonymous method is a method definition which doesn't contain a method name, that can be assigned to a delegate type. These were introduced in C# 2.0. It is created by using a delegate operator, which can be converted to a delegate type.
  • A Lambda Expression is a simpler and more expressive way of defining functionality which were introduced in C# 3.0. It is a set of arguments with the body which are separated by a lambda declaration operator (=>)
delegate void Del(string str);

// declaring an anonymous method
// assigning to a Delegate type Del
Del d1 = delegate (string str)
{
    Console.WriteLine($"Hello {str}");
};

// declaring a lambda expression
// assigning to a Delegate type Del
Del d2 = str =>
{
    Console.WriteLine($"Hello there! {str} via a Lambda Expression.");
};

What is the difference between Func and Action delegates in C#?

  • Func and Action are two delegate types defined in C# which accept one or more generic arguments
  • Both can accept upto 16 generic arguments.
  • The difference between Func and Action is in their return type.
  • Func type accepts one or more parameters and returns a value
  • Action type accepts one or more parameters but doesn't return a value.
Func<int, int> Sum = delegate (int a, int b) { 
  return a + b; 
}

Action<int, int> PrintSum = delegate (int a, int b) {
   Console.WriteLine(a+b);
}

// prints 9
Console.WriteLine(Sum(5,4));
PrintSum(5,4);

What are Delegates? How do you use them?

  • A Delegate is a type which can help passing a function as a parameter - similar to pointer functions.
  • These are derived from the Delegate class in .NET and are sealed types.
  • The type of the delegate is determined by the name of the delegate.
  • Methods passed into the delegate type must have the same signature as the defined delegate type.
  • There are three steps:
    1. Declare a delegate type
    2. Instigate the type by associating with a method definition
    3. Invoke it
  • Since the instantiated delegate is an object, it can be passed as a parameter, or assigned to a property. This allows a method to accept a delegate as a parameter, and call the delegate at some later time. This is known as an asynchronous callback, and is a common method of notifying a caller when a long process has completed.
void StartingPoint() 
{
     CallDelegate("HipHop", d1);
     CallDelegate("HipHop", d2);
}

// call the passed Del reference 
// with the passed parameter
void CallDelegate(string str, Del d)
{
    // Step 3: Calling the delegate
    d(str);
}

// Step1: declare a delegate type Del 
delegate void Del(string str);

// Step 2: Instigation
Del d1 = delegate (string str)
{
    Console.WriteLine($"Hello {str}");
};

// Step 2: Instigation
Del d2 = str =>
{
    Console.WriteLine($"Hello there! {str} via a Lambda Expression.");
};

Output:
Hello HipHop
Hello there! HipHop via a Lambda Expression.

How to Redis Caching in ASP.NET Core

In this article, let's talk all about caching in general and integrating & working with Redis Cache in an ASP.NET Core application.

What are the differences between a Stack memory and a Heap memory?

  • Stack memory is an array of memory, where the data is stored and removed (when no longer in use) in a Last-In-First-Out (LIFO) model.
  • Value Types (bool, byte, char, decimal, double, enum, float, int, long, sbyte, short, struct, uint, ulong, ushort) are stored in Stack memory.
  • Method calls are generally executed over a Stack and all the variables (of Value Types) are allocated inside the Stack. Once the control goes out of the Method, all the variable memory allocations are removed from the Top in LIFO order.
  • The values are stored and accessed directly, and is significantly faster than a Heap.
  • Heap memory is an area inside the memory, where the data is stored in the form of chunks of spaces.
  • Data can be stored and removed in any order, but the access is significantly slower.
  • Reference Types such as Strings, Lists, Collections and Class objects are stored in Heap memory
  • The contents of Heap Memory are managed by the Garbage Collector.

which loop runs faster – for loop or foreach?

  • A for-loop is a simple looping control statement in the C# language, which can be used to iterate over indexed data structures - such as Arrays.
  • A foreach-loop is a looping control statement which can be used to iterate over a collection that derives from the IEnumerable interface - that provides the Enumerator.
  • Conceptually, looping over an indexed data structure is faster than looping over a collection using an Enumerator - because the for-loop is simply picking up the values inside the indexes and this is equivalent to picking the value from a memory location - simple and straight forward.
  • A for-loop statement can't run on data-structures that are not indexed - such as Dictionaries.
  • A foreach-loop statement internally calls on the Enumerator and seeks through the collection, which has a slight overhead. There is an overhead for initializing the Enumerator for the collection, which is not required in a traditional for-loop.
  • But a foreach-loop has a cleaner and intuitive syntax when compared with the for-loop syntax and is generally preferred for iterating over most of the collections types.