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 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.

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.