MUST Have C# Interview Questions and Answers – Part 2

By | April 2, 2017
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest

This C# Tutorial Part-2 in series of C# Interview Questions with practical and detailed answers. In previous part, we covered many basic to intermediate level interview questions on C# with the help of practical examples. I’ll recommend strongly to go through part-1 in this series. We will continue this series to provide more practical details with real time scenarios and complete source code to grasp C# language concepts. But this time, we are understanding in a different way by comparing different concepts.C# Interview Questions and Answers

C# Interview Questions PDF version will be available later for download.


Following Technical Interview Questions and Answers will also be helpful.


Start Date: Aril 01, 2017 12:00 AM PST
End Date: May 01, 2017  6:00 AM PST

Udemy Courses Online

Take All Online Courses – Lifetime Access

Complete C# Interview Questions List

Managed Vs Unmanaged Code

Managed Code:

The code (like Vb.Net,C#,J#) which is written in .Net Framework and developed by .Net Framework and under the control of CLR(Common Language Runtime). And the garbage collector run automatically in managed code.

Unmanaged Code:

The code which is developed outside of the .Net framework is called unmanaged code. Unmanaged code does not run under the control of CLR. Unmanaged code executed with help of wrapper class CCW (COM Callable Wrapper) and RCW(Runtime Callable Wrapper). C++ can be used to write such application.

Back to top

Struct Vs class

Structs and classes are the template or blue print that is used to create an object of a class.

Class Struct
Classes can be inherited Structs are not inherited
Classes are reference type Structs are value type
It could be null It could not be null
Class can be abstract It cannot be abstract
You can override the method within class You cannot override the method within Struct
  • A struct always contain public default parameter less constructor it can not contain the private parameter less constructor.
  • Static constructor is triggered in case of class but not in case of structure.
    Output: This is Constructor B()

Back to top

Abstract Vs Interface

Abstract class and interface both are used to achieve abstraction (for hiding the functionality). Both contain the abstract method.

Abstract Class Interface
Abstract class contain both abstract and non-abstract method Interface contain only abstract method
It contain final, non-final,static,non-static variable It contain only static and final variable
Abstract class provides the implementation of interface It cannot provide implementation of abstract class
It does not support multiple inheritance Interface supports multiple inheritance
Abstract keyword is used to declare abstract class Interface keyword is used to declare the interface

Example of an Abstract class:

Example of Interface:

Back to top

Boxing Vs Unboxing

In C# there is three types of data type Value type,Reference Type and pointer type. The process of the converting a value type into reference type is called Boxing. While the Reference type is converting into value type called unboxing.

Example of Boxing and Unboxing:

Back to top

Method Overloading Vs Method Overriding

Method Overloading Method Overriding
Method Overloading is used for increasing the code readability Method Overriding is used for code re-usability
Parameter must be different Parameter must be same
It can perform within the class It occurs in two classes using inheritance (parent and child class)
It perform compile time polymorphism It perform runtime polymorphism
No special keyword to use for method overloading. It can perform simply within the class. Using virtual keyword with the base class method and override keyword with derived class method can achieved method overriding.

Example of Method Overloading:

Example of Method Overriding:

Back to top

Break and Continue Statements

Break statement is used to break out of loop that’s mean you can jump out of loop at the current position and it terminate the loop.

Continue statement is used to (one iteration) jump over one iteration and again it continues the loop.

Back to top

Constant Vs Read-Only

Constant and Read-only is the keyword that is used to make the field constant that can not be modified.

Constant Value:

Constant variable or local variable are assigned at the time of declaration they cannot be modified after the declaration. Constant variables are static by default hence you cannot use static keyword with the constant field (or variable).

ReadOnly:

A read-only field can be assigned either at the time of declaration or inside the constructor of the same class, that’s why read only field can be used run-time constant.

Back to top

Ref and out parameters

Ref and out parameters are used to pass an argument into a method. By using this parameter you can return more than one value.

In the case of ref parameter first you need to initialize it then pass to the method but for out parameter, you don’t need to initialize.

Ref:

The ref keyword is used to pass an argument as a reference. When the value of that parameter will be changed in the method, it will be reflected in the calling method.

It needs to be initialized before passing into the method.

Out:

The out keyword is used to pass an argument same as ref keyword but in the case of out ref, you can pass the argument without assigning the value. Instead, it must be initialized in the called method before it returns back to the calling method.

Back to top

String Vs String Builder

String:

String is Reference type data type, you cannot insert, modify or delete any data when a string is created that means in simple word we can say that string is an immutable means once you created the object of string you cannot modify it when you try to modify it will create a new object in memory.

String Builder:

String Builder is an object of string to work with string we have to use a namespace(“Using System. Text) so unlike string, a string builder is an object and we can apply insert, modify and remove function in string builder without creating a new object, It will automatically update in memory does not create new space in memory. We can also say that string builder is mutable that’s mean changeable.

Back to top

Array.copyTo() Vs Array.Clone()

Array.copyTo () and Array. Clone() both areused to replicate the element of existing Single dimension array.

Array.copyTo:

Array.copyTo () method copy the array’s element into existing/destination array with the reference of targeted instance of existing/destination array. You can say that Array.copyTo allows to replicate the into specified index.

Array.Clone():

Array. Clone () replicate the existing element array in the new array object containing all element of exiting array without having any reference.

Back to top

Dispose and Finalise methods

Memory management is a big challenge when you are developing a web Application/software as we know that Garbage Collector plays the main role in .Net to manage unmanaged resource and Dispose and finalize method both are part of Garbage Collector.

Dispose and finalize method both are used to reclaim the memory used by the unmanaged resource.

Dispose method Finalize method
Unlike Finalize method it is not directly called by GC, User has to write code to implement it. Finalized method is automatically called by GC when any resource goes out of scope or at end of program.
It is a faster method and called by the user for instant object disposal. It is a slower method and automatically called by GC at end of,User can only override method when indeed.
Dispose method is the part IDisposable interface in GC Finalize method is the part of object class in GC
Back to top

Late binding Vs Early binding

If the methods have same name and different parameter declare in a single class or methods have same name and same parameter but declare in different classes then it is called Binding or Polymorphism.

Late Binding:

It is also called runtime polymorphism; the situation arrives when methods are virtual and have same name and same parameter in different classes (must be derived class). Then it is called method overriding or late binding.

Late Binding:

It is also called compile time polymorphism; the situation arrived when methods have same name but different parameter in same class.

It is also called method overloading.

Back to top

IEnumerable & IQueryable

IEnumerable and IQueryable are used for data manipulation in LINQ from the database and collection.

IEnumerable IQueryable
It exists in System. Collections Namespace It exists in System.Linq Namespace
It is the best for query data from in memory collections like List, Array etc. It is best for query data from out-memory (like remote database, Service) collection.
It execute select query on server side, load data in memory on client side and then filter data. It executes select query on server side as well as filtered there.
Suitable for LINQ or Object and LINQ to XML queries Suitable for LINQ to SQL queries
It does not supports the custom query It supports custom query using Create Query and Execute method
It supports lazy loading that’s why suitable for paging It does not support lazy loading that’s why not suitable for paging
Back to top

Throw Exception Vs Throw Clause

Throw Exception Throws Clause
It is used to throw an exception explicitly It is used to declare an exception
It is used inside the method body to invoke an exception. It is used in method declaration(signature)
You cannot throw more than one exception You can throw multiple exceptions
Follow by instance variable Follow by exception class name

Throw Exception Example:

Throws Clause Example:

Back to top

== Vs equals

The == operator compare the reference of the object while the equals compares the contents of the object see the bellow example.

Back to top

Is Vs as operator

Is operator is used to checking the Object type and it returns the Boolean value true if the object is same otherwise returns false.

The as operator perform the conversions between compatible types.it is also work similar as Is operator but instead of return Boolean value it returns the object if they are compatible type otherwise returns null.

Example of Is Operator:

Example of as Operator:

Back to top

Array Vs Array List

Array Array List
Array is collection of similar data types that’s mean we can store only one data type either Int,string,char….etc. Array List is the collection of different data types that’s mean we can store all data types in array list.
The length of array is fixed Array list can decrease or increase dynamically as per usage
Array belongs to System. Array namespace It belongs to System. Collection namespace

Array Example:

ArrayList Example:

Back to top

Directcast Vs ctype

Directcast cType
Directcast is generally used to cast reference data type. It is used to cast value type.
To perform the Directcast between two different classes, the classes should have relationship between them. To perform ctype b/w two different value types, relationship is not required. If the conversion is legal it will be performed.
When you perform Directcast on arguments that do not match it will throw InvalidCastException. Exception is not thrown while perform ctype.
Performance of Directcast is better than ctype because there is no runtime helper routines of VB.Net are used for casting. It requires runtime helper routine of VB.Net hence performance is slow that Directcast.
Directcast is portable across many language that’s mean it is not specific to VB.Net Ctype is not portable it is specific to VB.Net.

Directcast Example:

Type Cast Example:

Where obj is  Typecast

Back to top

Public Vs Static Vs void

Public: public keyword is access modifier and tells the c# compiler that the Main method is accessible by anyone.

Static: static keyword declared that the Main method is global and can call without creating an instance of the class.

Void: void keyword is used to specify that the Main method does not return any value.

Example of public and static keyword:

Back to top

Top Technical Interview Questions and Answers Series:

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest