Groundwork For .NET Interview

At the initial stage, any company finds the right person for the position. That's why their main focus is to evaluate the interviewee's education, personality, experience, skill and knowledge etc. We know many things; but we can focus on the technologies that we need for day to day work. Generally we forget many things that we rarely use. This article will cover very limited concepts on .net basic.

Coverage Topics 

  • Access level of access modifiers
  • Access modifiers for types and type members
  • Default access modifier for class and its members
  • IEnumerable vs. IEnumerator
  • IEnumerable vs. IQueryable
  • Single method interface as IComparable vs. IComparer
  • Variable declared as const vs. readOnly
  • Default value expressions
  • Passing parameters to methods and return value
  • ref return values and ref locals
  • Sealed class and sealed method
  • EF Core development approaches
  • Compile-time vs. run-time polymorphism
  • Abstract method vs. virtual method
  • Abstract class vs. interface
  • Application domain
  • Serialization vs. deserialization
  • Use of stream
  • Http post vs. put
  • Process vs. thread
  • The mechanisms to run code in parallel
  • Synchronization mechanisms in threads

Prerequisites

Job Titles and Responsibilities

Job titles and job responsibilities are differed from company to company. In the interview, job responsibilities and experience are important for the position. For the Programmer position with a year of experience they may focus on the oops concept, parallel programing, algorithm and problem-solving skills etc. On the other hand, if the position required couples of years experiences then they could focus on latest frameworks, languages and its features, unit testing concept, scenario base problems, software development approaches, design principles, design patterns, best practice guidelines and software architectures etc.

.NET 

Drill down the basic .net concept,

Access level of access modifiers

.NET 

Private

Access limited within the class that contains it. In the below diagram, privateA is accessible within only ClassA and rest of the classes can't access it.

Protected

Accessible within the containing class and also accessible all classes which are derived from this class. For example, ProtectedB of classA is accessible inside the containing ClassA and also accessible from derived class ClassB in the assembly Assembly-A. It is also accessible from derived class ClassE in another assembly Assembly-B.

.NET

Note
Don't judge the class diagram which is presented differently, just to focus on the access modifiers and the relation of the classes.

Internal

Accessible to all classes declared in the same assembly. For example, InternlC of ClassA is accessible inside the containing class ClassA and also accessible within the same assembly Assembly-A by any classes ClassB and ClassC.

Private protected (C# 7.2)

Accessible by any derived classes in the same assembly. For example PrivateProtectedD of classA is accessible inside the containing class ClassA as well as accessible from the derived classes classB in the Assembly-A.

Protected internal

Accessible to all classes declared in the same assembly or from derived classes in another assembly. For example, ProtectedInternalE of ClassA is accessible from all classes ClassA, ClassB and ClassC in the same assembly Assembly-A OR from derived class ClassE in another assembly Assembly-B.

Public

Accessible by anyone. For example, PublicF of ClassA is accessible from all classes in the assembly-A or Assembly-B.

Access modifiers for types and type members,
  • Types (class, struct, enum, interfaces, delegate etc.) can have only internal and public access modifiers.
  • Type members (field, property, constructor, method etc.) of the type can have all the access modifiers.
Default access modifier for class and its members
.NET 
If no access modifier is specified then,
  • internal is default for the classes,
  • private is default for the members.
IEnumerable vs. IEnumerator

These are used forward and read only access to a collection.

.NET 
  • IEnumerable uses IEnumerator and it can be used with foreach statement.
  • IEnumerator has MoveNext, Reset methods and Current property. It can be used with while statement.
    1. List < string > stringList = new List < string > () {  
    2.     "Rony",  
    3.     "Anna",  
    4.     "Jhon"  
    5. };  
    6. //// IEnumerator  
    7. Console.WriteLine("IEnumerator:\n");  
    8. IEnumerator < string > enumeratorStringList = stringList.GetEnumerator();  
    9. while (enumeratorStringList.MoveNext()) {  
    10.     Console.WriteLine(enumeratorStringList.Current);  
    11. }  
    12. //// IEnumerable  
    13. Console.WriteLine("\nIEnumerable:\n");  
    14. IEnumerable < string > enumerableStringList = stringList;  
    15. foreach(string item in enumerableStringList) {  
    16.     Console.WriteLine(item);  
    17. }  
IEnumerable vs. IQueryable

Both of them can be used moving forward with data access.

.NET
 

IEnumerable

  • Good to query data from in memory collections (say, list)
  • It loads data (server side to client side) in memory while query data from database and then it filters data on client side.
  • Doesn’t support custom query.
  • Doesn’t support lazy loading.
  • Good for LINQ-to-Object or LINQ-to-XML.

IQueryable

  • Good to query data from out memory (server-side like, remote database, web service)
  • It filters data on the server side while query from the database and then it send to client side. It improves performance.
  • Supports custom query (CreateQuery, Execute method).
  • Supports lazy loading.
  • Good for LINQ-to-SQL
Single-Method Interface: IComparable vs. IComparer

Both of them can be used for custom sorting in the collections. The main difference is IComparable allows internal sort implementation while IComparer allows external customized sort implementation. 

.NET 

IComparable

.NET 
.NET 

IComparer

.NET 
IComparer using lambda expression,

  1. personObjList.Sort((x, y) => x.Name.CompareTo(y.Name));  
  2. personObjList.Sort((x, y) => x.Age.CompareTo(y.Age));  
IComparer on multiple properties in the class,
  1. public class PersonComparer: IComparer < Person > {  
  2.     public enum SortBy {  
  3.         FirstName,  
  4.         Age  
  5.     }  
  6.     private SortBy sortBy;  
  7.     public PersonComparer(SortBy sortBy) {  
  8.         this.sortBy = sortBy;  
  9.     }  
  10.     public int Compare(Person x, Person y) {  
  11.         switch (this.sortBy) {  
  12.             case SortBy.FirstName:  
  13.                 return x.FirstName.CompareTo(y.FirstName);  
  14.             case SortBy.Age:  
  15.                 return x.Age.CompareTo(y.Age);  
  16.             default:  
  17.                 return x.FirstName.CompareTo(y.FirstName);  
  18.         }  
  19.     }  
  20. }  
  21. //// How to use PersonComparer  
  22. personObjList.Sort(new PersonComparer(PersonComparer.SortBy.Age));  
  23. //// personObjList.Sort(new PersonComparer(PersonComparer.SortBy.FirstName));  
Variable declared as const vs. readOnly
 
.NET 
Const

Const is initialized at the time of declaration. Otherwise, it will throw a compilation error. It is known as compile-time constant. It can't be changed at run-time. By default, it is static.

ReadOnly

It can be initialized at the time of declaration or multiple-times it can be set in the constructor of the same class. It is known as run-time constant.

Default value expressions

.NET

Passing parameters to methods and return value
 
.NET 
  • ref uses to pass variable for input and output; It is initialized before passing.
  • in (C# 7.2) uses to pass variable only for input; It is initialized before passing.
  • out uses only for output; It is initialized inside method.
Example
  1. internal class ParameterPassing {  
  2.     public void ParameterPassingExample(int valueA, ref int inputAndOutputValueB, in int inputOnlyValueC, out int outputOnlyValueD) {  
  3.         inputAndOutputValueB = valueA + inputAndOutputValueB;  
  4.         outputOnlyValueD = inputAndOutputValueB + inputOnlyValueC;  
  5.     }  
  6. }  
  7. static void Main(string[] args) {  
  8.     ////******************* Call *********************////  
  9.     int valueA = 5;  
  10.     int inputAndOutputValueB = 6;  
  11.     int inputOnlyValueC = 7;  
  12.     int outputOnlyValueD;  
  13.     ParameterPassing parameterPassing = new ParameterPassing();  
  14.     parameterPassing.ParameterPassingExample(valueA, ref inputAndOutputValueB, in inputOnlyValueC, out outputOnlyValueD);  
  15. }  
ref returns on method and ref locals

.NET 
ref return returns the storage location and ref locals can store that in a local variable. But ref readonly locals(C# 7.2) doesn't allow writes to that object instead of read value.

.NET

Sealed Class and sealed method
  • sealed class can't be inherited
  • sealed method can't be override in a derived class

Example of sealed class,

.NET 

Example of sealed method,

.NET 
 
EF Core development approaches

Entity Framework (EF) Core 2.0 doesn't support visual designer or wizard for DB model (edmx). EF Core supports only two development approaches,

  • Code-First
  • Database-First.
.NET
Compile time vs. run time polymorphism

Polymorphism (means one name, multiple forms) one interface and many implementations.

Binding/type of polymorphism

Binding is the connection of a method call to a method implementation.

 .NET

Compile-time polymorphism (early-binding/overloading/static binding)

Method overloading

The same name of the methods in the same class take multiple implementation forms.

.NET
Operator overloading
 
.NET 

Run-time polymorphism (late-binding/overriding/dynamic-binding)

It is implemented using inheritance and virtual method.

.NET 
Abstract method vs. virtual method
  • Virtual method has default implementation as well as provides the derived class with an option of overriding it.
  • Abstract method doesn’t provide default implementation and forces the derived class to override the method.
.NET
Abstract class vs. interface
  • Accessibility modifier (public/internal etc.) is allowed for abstract class. Interface doesn't allow accessibility modifier.
  • Class can inherit only one abstract class; class can implement more than one interface.
.NET 
  • Abstract classes can have default implementations for some of its (virtual) members (methods), but the interface class holds only the signature. It can't have implementation for any of its members. But C# 8(not release yet) supports default implantation of the methods in the interface.

    Example,
.NET 
Application domain

Application Domain provides a logical isolated boundary of the application for security. All the objects of the same application are created within the same application domain. Application Domain keeps assemblies separate within a single process.

Serialization vs. deserialization
  • Serialization: Transforming object to XML string.
  • Deserialization: Transforming XML string to object.
More details click here,

.NET 

Use of stream

When data size is too large then it is very difficult to load whole data into the memory at the same time. Stream is used to read data from large file. You can read small chunks of data where the large file is broken down into small chunks.

Http post vs. put
  • Post is used to create new entity
  • Put is used to update an existing entity.
Process vs. thread

Threads run in a shared memory space, while process run in separate memory spaces.

.NET 

When you double-click on the Outlook icon, you start the application in OS which is a process. The process is an executing instance of an application.

You can consider “auto spelling & grammar check” and “auto check names” as threads in Outlook. A thread is a path of execution within a process.

The mechanisms to run code in parallel

  .NET

Example of the simple signatures,
 
.NET 
Synchronization mechanisms in threads

When multiple threads share resources (share data) then it might produce problems. The producer-consumer and the reader-writer problems are the most common example. To avoid the problem, we need to synchronize to access the data.

.NET 
 
The source code is added on the top of this article. Find the attachment.