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.

What is an IEnumerable?

  • IEnumerable is an interface available in the System.Collection namespace
  • Any collection that implements the IEnumerable implements the GetEnumerator method of the IEnumerable interface, making it iterable.
  • The IEnumerable or the Generic IEnumerable must be used over in-memory objects.

How can you compile a .NET Project without using Visual Studio?

  • We can compile .NET Framework projects without using Visual Studio on the Command Line using csc.exe file.
  • We can supply additional arguments to csc.exe, in-addition to the csproj file of the .NET project.
  • In .NET Core, we use the dotnet build command on the command-line which can build the csproj without using Visual Studio.

What is JIT Compilation?

  • A JIT (Just-In-Time) compiler converts the MSIL (MS Intermediate Language) into native code on demand during the application runtime, when the application components are loaded and executed.
  • The JIT compilation happens specific to the running CPU architecture, since the common language runtime supplies a JIT compiler for each supported CPU architecture.
  • If the managed code calls platform-native APIs or runs platform-specific assemblies, the JITed code runs on only that particular system.

What is a Tuple? How many values can a Tuple Hold?

  • A Tuple is a data structure which can hold data of multiple types in a single variable.
  • Tuple was first introduced in .NET 4.0.
  • A Tuple can generally represent a row in a database table, where each column can represent a different type.
  • Individual values of "Items" inside a single tuple variable can be accessed by means of properties (Item1, Item2, ..)
  • A tuple can hold a maximum of 8 Items in itself, where the individual Items can themselves be Tuples of different items.
var tuple = new Tuple<T1, T2, T3, ... , T7, T8>();

What is GAC in .NET?

  • GAC stands for Global Assembly Cache
  • It is a place in the Windows directory where the assemblies (the DLL files) designated to be shared by the applications are stored for reuse.
  • Any assembly or component which is desired to be shared across applications in a machine is registered on the Global Assembly Cache.
  • To register an assembly into the Global Assembly Cache, we can do so by using the GACUtil.exe as below:
> gacutil -i [Path][Assembly Name].dll

What are Namespaces?

Namespaces are used to group and organize classes which are somewhat related to each other. Any class created would reside under a namespace and is referenced using the namespace in other classes or modules.

namespace MyModule {
  class MyClass {
   // all things under class
  }
  class MyOtherClass {
   // all things under class
  }
}