Can you unit test a private method? How do you do it?

The answer is No. We cannot unit test a private method.

  • Unit Tests are designed to test the functionalities of components which are exposed to other components for consumption.
  • Private methods are designed to hide functionality from the other components as a part of data abstraction.
  • So its not a good practice to try unit testing a private method.
  • We can instead try unit testing a public method which calls this private method and assert the overall expectation.

What are extension methods? When do you use them?

  • Extension methods help extending types without altering them
  • They're particularly useful for extending classes which are sealed
  • They're created as static methods inside static classes
  • The first parameter of the extension methods is the type for which the method is to extend.
  • If the return type of the extension method is the type they're extending, then the methods can be chained together.
public class MyClass {
  /// 
}

public static MyClassExtensions {
  public static MyClass DoExtended(this MyClass obj, int someparam1, int someparam2.. )
  {
      // do something
      return obj;
  }
}


MyClass obj = new MyClass();
obj.DoExtended(); // this works although DoExtended is not a part of MyClass

What is closure in Javascript?

  • In Javascript, a closure can be imagined as a concept of a function scope within a function.
  • The global execution context can be treated as a whole function in which all the script contents are present, and when we run the script the global function is called.
(function () {
    let k = 1;
    function kd() {
        console.log(`value of k is ${k}`);
    }
    kd();
})();

output:
value of k is 1
  • the inner functions present in this function can access all the variables that are defined in the outer scope and can modify them.
  • interestingly, these variables are still available even after the outer function has been executed and removed from memory.
function outer() {
    let counter = 0;
    function inner() {
        counter++;
        return counter;
    }
    return inner;
}

let instance = outer();
console.log(instance());
console.log(instance());
console.log(instance());

output:
1
2
3
  • In the above code block, the outer function is called only once, while when we call the inner function multiple times the variable counter which is native to the outer function is modified by the inner function and value persists.
  • This is conceptually similar to the context of methods and properties in a class where the methods can access and modify the variables of the class in an Object Oriented Language.

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);