C#(CSharp) Interview Questions & Answers) - Part 2.

C#(CSharp) Interview Questions & Answers) - Part 2.

This article is continuation to the previous C# Interview Q & A (Part 1). In this article we will look into further 20 basic C# interview questions.

Also see our YouTube C# Interview Questions video for elaborated answers.

Q31. What is Delay signing?

The whole point about strong names is to ensure that the clients (UI , External components etc) who is consuming the DLL knows that the DLL was published from a valid source. This authenticity is verified by using strong names. Strong name protection is good from external hackers but what if your own developers think of doing something mischievous.

That’s where delay signing helps. The strong name key has two keys public key and private key. You only share the public key with your developers so that they can work seamlessly. The private key is stored in a secured location and when the DLL is about to be deployed on production the key is injected for further security.

Q32. What is GAC?

GAC (Global Assembly Cache) is where all shared .NET assembly resides. GAC is used in the following situations:-

  • If the application has to be shared among several application which is in the same computer.
  • If the assembly has some special security, requirements like only administrators can remove the assembly. If the assembly is private then a simple delete of assembly the assembly file will remove the assembly.

Q33. How to add and remove an assembly from GAC?

You can use the ‘GacUtil’ tool which comes with visual studio. So to register an assembly in to GAC go to “Visual Studio Command Prompt” and type “gacutil –i (assembly name)”, where (assembly name) is the DLL name of the project.

One you have installed the assembly the DLL can be seen in ‘c:\windows\assembly\’ folder.

When we have many DLL’s to be deployed we need to create setup and deployment package using windows installer. So the common way of deploying GAC DLL in production is by using windows installer.

Q34. If we have two versions of the same assembly in GAC how to we make a choice?

When we have two version of the same assembly in GAC we need to use binding redirect tag and specify the version we want to use in the new version property as shown in the below “app.config” file.

No alt text provided for this image

Q.35 What is Reflection and why we need?

Reflection is needed when you want to determine / inspect contents of an assembly. For example look at your visual studio editor intellisense, when you type “.” (dot) before any object , it gives you all members of the object. This is possible because of reflection.

No alt text provided for this image

Reflection also goes one step further; it can also invoke a member which is inspected. For instance if the reflection detects that there is a method called as “GetChanges” in an object. We can get a reference to that method instance and invoke the same on runtime.

In simple words reflection passes through two steps “Inspect” and “Invoke” (optional). “Invoke” process is optional.

No alt text provided for this image

Q.36 How do we implement reflection?

Implementing reflection in c# is a two step process , 1st get the “type” of the object and then use the type to browse members like “methods” , “properties” etc.

Step 1:- The first step is to get the type of the object. So for example you have a DLL called as “ClassLibrary1.dll” which has a class called as “Class1”. We can use the “Assembly” (belongs to System.Reflection namespace) class to get a reference to the type of the object. Later we can use “Activator.CreateInstance” to create an instance of the class. The “GetType()” function helps us to get reference to the type of the object.

No alt text provided for this image

Step 2 :- Once we have reference the type of the object we can then call “GetMembers” or “GetProperties” to browse throw the methods and properties of the class.

No alt text provided for this image
No alt text provided for this image

In case you want to invoke the member which you have inspected you can use “InvokeMember” to invoke the method. Below is the code for the same.

No alt text provided for this image

Q.37 What are the practical uses of reflection?

  • If you are creating application like visual studio editors where you want show internal of an object by using intellisense.
  • In unit testing sometimes we need to invoke private methods. That’s a different thing test private members are proper or not.
  • Sometimes we would like to dump properties, methods and assembly references to a file or probably show it on a screen.

Q.38 What is the use of Dynamic keyword?

Programming languages can be divided in to two categories strongly typed and dynamically typed. Strongly typed languages are those where the checks happen during compile time while dynamic languages are those where type checks are bypassed during compile time. In dynamic language object types are known only during runtime and type checks are activated only at run time.

No alt text provided for this image

So we would like to take advantage of both the world. Because many times we do not know object type until the code is executed. In other words we are looking at something like dynamically statically typed kind of environment. That’s what dynamic keyword helps us with. If you create a variable using the “Dynamic” keyword and if you try to see members of that object you will get a message as shown below “will be resolved at runtime”.

No alt text provided for this image

Now try the below code out. In the below code I have created a dynamic variable which is initialized with a string data. And in the second line I am trying to have fun by trying to execute a numeric incremental operation. So what will happen now?.... think.

No alt text provided for this image

Now this code will compile fine without any complains. But during runtime it will throw an exception complaining that the mathematical operations cannot be executed on the variable as it’s a string type. In other words during runtime the dynamic object gets transformed from general data type to specific data type (ex: - string for the below code).

No alt text provided for this image

Q.39 What are practical uses of Dynamic keyword?

One of the biggest practical uses of dynamic keyword is when we operate on MS office components via interop. So for example if we are accessing Microsoft excel components without dynamic keyword , you can see how complicated the below code is. Lots of casting happening in the below code, right.

No alt text provided for this image

Now look at how simple the code becomes by using the dynamic keyword. No casting needed and during runtime type checking also happens.

No alt text provided for this image

Q.40 What is the difference between Reflection and Dynamic?

  • Both reflection and dynamic are used when we want to operate on an object during runtime.
  • Reflection is used to inspect meta-data of an object. It also has the ability to invoke members of an object on runtime.
  • Dynamic is a keyword which was introduced in .NET 4.0. It evaluates object calls during runtime. So until the method calls are made compiler is least bothered if those methods / properties etc exist or not.
  • Dynamic uses reflection internally. It caches the method calls made thus improving performance to a certain extent.
  • Reflection can invoke both public and private members of an object while dynamic can only invoke public members.

Below is the detail comparison table which shows in which scenario they are suited

No alt text provided for this image

Below is a simple diagram which summarizes visually what reflection can do and what dynamic keyword can do?

No alt text provided for this image

Q.41 Explain the difference between early binding and late binding?

Early binding or early bound means the target methods, properties and functions are detected and checked during compile time. If the method / function or property does not exist or has a data type issues then the compiler throws an exception during compile time.

Late binding is opposite of early binding. Methods, properties and functions are detected during runtime rather than compile time. So if the method, function or property does not exist during runtime application will throw an exception.

Note :- In simple words everything is early binded until you are using reflection or dynamic keyword.

Q.42 What is the difference between VAR and Dynamic keyword?

Note :- Comparing VAR and Dynamic keyword is like comparing Apples and oranges. Many people confuse VAR with the variant datatype of VB6 and there’s where interviewer tries to confuse you. But there is absolute no connection of var c# keyword with variant of VB6.

VAR is early binded (statically checked) while DYNAMIC is late binded (dynamically evaluated).

VAR keyword looks at your right hand side data and then during compile time it decides the left hand side data type. In other words VAR keyword just saves you typing lot of things.

No alt text provided for this image

On the other hand dynamic keyword is for completely different purpose. Dynamic objects are evaluated during runtime. For instance in the below code the “Length” property exists or not is evaluated during runtime.

No alt text provided for this image

If for some reason you mistype the “Length” property to “length” (Small “l“) you would end up in to a runtime exception as shown in the below figure.

No alt text provided for this image

So in short sentence VAR does static check while DYNAMIC evaluates and checks during runtime.

Q.43 Explain term type safety and casting in C#?

There are two types of languages one which is type safe and one which is not. Type safety means preventing type errors. Type error occurs when data type of one type is assigned to other type UNKNOWINGLY and we get undesirable results.

For instance JavaScript is a NOT a type safe language. In the below code “num” is a numeric variable and “str” is string. JavaScript allows me to do “num + str” , now GUESS will it do arithmetic or concatenation .

Now for the below code the results are “55” but the important point is the confusion created what kind of operation it will do.

This is happening because JavaScript is not a type safe language. Its allowing to set one type of data to the other type without restrictions.

No alt text provided for this image

C# is a type safe language. It does not allow one data type to be assigned to other data type. The below code does not allow “+” operator on different data types.

No alt text provided for this image

But in certain scenarios we would like to convert the data type to achieve the given results that’s where we need to do conversion or casting. The next question talks about the same in detail.

Q.44 Explain casting, implicit conversion and explicit conversion?

Note :- Some of interviewers term this conversion as casting and some just prefer to call it as conversion , at the end of the day they mean the same thing.

To understand implicit and explicit conversion of data types first let’s try to understand type casting concept. Type casting is a mechanism where we convert one type of data to other type.

For example below is simple code where we want to move integer (value without decimals) value to a double (value with decimals). When we try to move double data type to integer data type casting occurs. Casting is also termed as conversion.

No alt text provided for this image

In the above example there is no loss of data. In other words when we moved 100 integer value to double we get the 100 value as it. This is termed as implicit conversion / casting.

Now consider the below code where we are trying to move a double to an integer. In this scenario in integer only 100 will be received decimals will be removed. You can also see I need to explicitly specify that we need to convert in to integer. This is termed as explicit conversion.

No alt text provided for this image

Q.45 What are stack and heap?

Stack and heap are memory types in an application. Stack memory stores data types like int , double , Boolean etc. While heap stores data types like string and objects.

For instance when the below code run the first two variables i.e. “i” and “y” are stored in a stack and the last variable “o” is stored in heap.

No alt text provided for this image

Q.46 What are Value types and Reference types?

Value types contains actual data while reference types contain pointers and the pointers point to the actual data.

Value types are stored on stack while reference types are stored on heap. Value types are your normal data types like int , bool , double and reference types are all objects.

Q.47 What is concept of Boxing and Unboxing?

When value type is moved to a reference type it’s called as boxing. The vice-versa is termed as unboxing.

Below is sample code of boxing and unboxing where integer data type is converted in to object and then vice versa.

No alt text provided for this image

Q.48 How performance is affected due to boxing and unboxing?

When boxing and unboxing happens the data needs to jump from stack memory to heap and vice-versa which is a bit of memory intensive process. As a good practice avoid boxing and unboxing where ever possible.

Q.49 How can we avoid boxing and unboxing?

First thing it’s very difficult to avoid boxing and unboxing. For instance most of the time you will moving data from UI objects like text boxes etc to business objects and also vice versa which will demand boxing and unboxing. Below are some key points to remember:-

  • First thing is it really necessary to use boxing and unboxing. If it’s unavoidable like moving data from UI text boxes to internal c# data types, then go for it.
  • Try to see if you can use generics and avoid it.

Q.50 If we have a class referencing value type, where is value type stored?

The value type will be stored in a heap.

Happy Learning ..! Happy Job Hunting....!

Ibrahim Ahmed

An Experienced IT, Logistics and Supply Chain Specialist

4y

shivprasad koirala your contribution sir is highly respected and valuable.

Like
Reply
Sourabh Raizada

Full stack developer .Net | C# | Web Api | WCF | Angular 6 | SQL | Agile practitioner

4y

Very helpful

Like
Reply

Complete Interview Questions courses can be availed from here https://www.questpond.com/interview-questions---answers-tutorial/cid63 Happy Job Hunting...!!!

Like
Reply

To view or add a comment, sign in

Others also viewed

Explore topics