Sei sulla pagina 1di 5

C# Interview Questions

1. What is the difference between a struct and a class?


Structs cannot be inherited. Structs are passed by value and not by reference. Structs are stored on the
stack not the heap. The result is better performance with Structs.

2. What is a singleton?

A singleton is a design pattern used when only one instance of an object is created and shared; that is, it
only allows one instance of itself to be created. Any attempt to create another instance simply returns a
reference to the first one. Singleton classes are created by defining all class constructors as private. In
addition, a private static member is created as the same type of the class, along with a public static
member that returns an instance of the class.

3. Why to use “finally” block in C#?

“Finally” block will be executed irrespective of exception. So while executing the code in try block when
exception is occurred, control is returned to catch block and at last “finally” block will be executed. So
closing connection to database / releasing the file handlers can be kept in “finally” block.

4. What is boxing?

Boxing is the process of explicitly converting a value type into a corresponding reference type. Basically,
this involves creating a new object on the heap and placing the value there. Reversing the process is just
as easy with unboxing, which converts the value in an object reference on the heap into a corresponding
value type on the stack. The unboxing process begins by verifying that the recipient value type is
equivalent to the boxed type. If the operation is permitted, the value is copied to the stack

5. What is the difference between “out” and “ref” parameters in C#?


“out” parameter can be passed to a method and it need not be initialized where as “ref” parameter has
to be initialized before it is used.

6. How are methods overloaded?


Methods are overloaded via different signatures (number of parameters and types). Thus, you can
overload a method by having different data types, different number of parameters, or a different order
of parameters.

7. How do you prevent a class from being inherited?

The sealed keyword prohibits a class from being inherited.

8. Explain circular reference in C#?

This is a situation where in, multiple resources are dependent on each other and this causes a lock
condition and this makes the resource to be unused.
9. What are implementation inheritance and interface inheritance?
Implementation inheritance is achieved when a class is derived from another class in such a way that it
inherits all its members. Interface inheritance is when a class inherits only the signatures of the
functions from another class.

10. What are generics in?


Generic types to maximize code reuse, type safety, and performance. They can be used to create
collection classes. Generic collection classes in the System.Collections.Generic namespace should be
used instead of classes such as ArrayList in the System.Collections namespace.

11. What is the Constructor Chaining in C#?

Answer: constructor chaining is a way to connect two or more classes in a relationship as Inheritance, in
Constructor Chaining every child class constructor is mapped to parent class Constructor implicitly by
base keyword so when you create an instance of child class to it’ll call parent’s class Constructor without
it inheritance is not possible.

MVC Interview Questions

1. What is ASP.NET MVC?


ASP.NET MVC is a web development framework used for creating web applications on the ASP.NET
platform.It is an alternative to web forms for creating web applications.It is based on the Model View
Controller architectural pattern which used for creating loosely coupled applications.The three
main components in a MVC application are :

Model represents the business entities

View used to render the UI

Controller receives the end user request,performs operations on the model as required and renders the
view.

2. What are the advantages of MVC?


Separation of Concerns-SoC
MVC encourages to implement the application using separation of concerns.This means that you can
easily update either of the three components : model , view or controller without impacting the other
components.View can be changed without the need to change model or controller.Similarly Controller
or Business Logic can be updated easily without impacting the application.

Easy to Unit test


Since the components are independent so you can easily test the different components in isolation.
Full Control over the generated HTML
MVC views contains HTML so you have full control over the view that will be rendered.Unlike WebForms
no server controller are used which generates unwanted HTML.

3. Do we have ViewState in MVC?


This is a very commonly asked question.It lets the interviewer judge your understanding of MVC.One big
difference between WebForms and MVC is MVC does not have viewstate.The reason that MVC does not
have viewstate is because viewstate is stored in a hidden field on the page.So this increases the size of
the page significantly and impacts the page load time.

4. What are Action Methods?


Action methods are defined by the controllers ,urls are mapped to the action methods.
The request which is received by our mvc application is ultimately handled by an action method.Action
method generates the response in the form of ActionResult.The action method to execute is
determined according to the routing rules defined by our application.

5. What is ActionResult?
ActionResult is a class which represents the result of an action method.Action methods returns an
object of a type which derives from this ActionResult class.Since ActionResult is an abstract class so it
provides few derived classes whose object the action method can create to return the
response.Also there are few methods available to the controller class to create ActionResult subclass,so
we don’t need to explicitly create an object of the ActionResult and can just call the method.
Some of the classes deriving from the ActionResult are:

Action Result Helper Method Description

ViewResult View Renders a view

Renders a partial view, which is a view which can be used inside


PartialViewResult PartialView
another view.

RedirectResult Redirect Redirects to different action method as specified in the URL.

RedirectToRouteResult RedirectToRouteRedirects to another action method.

ContentResult Content Returns the user supplied content type.


JsonResult Json Returns a JSON object.

6. What are HTML helpers?


HTML helpers are methods which returns HTML strings.There are few inbuilt HTML helpers which we
can use.If the inbuilt helpers are not meeting our needs ,we can also create our custom HTML
helpers.They are similar to the webforms controls as both the webforms controls and the MVC HTML
helpers returns HTML.
But HTML helpers are lightweight compared to the webforms controls.

7. What are Filters?


Sometimes we want to execute some logic either before the execution of the action method or after the
execution of the action method.We can use Action Filter for such kind of scenario.Filters defines logic
which is executed before or after the execution of the action method.Action Filters are attributes which
we can apply to the action methods.Following are the MVC Filters:

Authorization filter
Action filter
Result filter
Exception filter

8. Which Filter executes last?


Exception filter executes last,after all other filters have executed.

9. What is Routing in MVC?


In WebForms the URL’s are mapped to the physical files on the file system.But in the case of ASP.NET
MVC URL’s are not mapped to the physical files but are mapped to the controllers and action
methods.This mapping is done by the routing engine.To map the URL to the controllers and action
methods ,the routing engine defines the routes.Based on the matching route the request is handled by
the appropriate controller.

10. Explain about Razor view engine?


View engine allows us to use server side code in web pages.This server side code is compiled on the
server itself before getting sent to the browser.
Razor and Web forms are the default view engines in MVC. Razor view have the
extension cshtml for view containing c# code and vbhtml for views containing vb.net code.
Razor has the following features:
Razor view define code blocks which starts with { and ends with }.Single or multiple statements can be
defined in the code block

Code block starts with @

Inline expressions can be mixed with normal html and are not required to start with @.

11. What is OutputCaching in MVC?


OutputCaching allows you to store the response of the action method for a specific duration.This helps
in performance as the saved response is returned from the action method instead of creating a new
response. Following action method will return the saved response for next 30 seconds.

12. What is a View Engine?


View Engines are responsible for generating the HTML from the views.Views contains HTML and
source code in some programming language such as C#. View Engine generates HTML from the
view which is returned to the browser and rendered.Two main View Engines are WebForms and
Razor ,each has its own syntax.

13. Mention how can maintain session in MVC?


Session can be maintained in MVC by three ways tempdata, viewdata, and viewbag.

14. Mention what is the difference between Temp data, View, and View Bag?
Temp data: It helps to maintain data when you shift from one controller to other controller.

View data: It helps to maintain data when you move from controller to view.

View Bag: It’s a dynamic wrapper around view data.

15. What is MVC 6?


In MVC 6 ,the three frameworks,WebAPI ,MVC and SingnalR are merged into a single framework.Also in
MVC dependency on System.Web is removed.

16. What is ASP.NET Core?


ASP.NET Core is a new version of ASP.NET.It is more than just another version of ASP.NET it is rewritten
for web and cloud applications.

These are some of the most important MVC interview questions and answers which you should know
when attending MVC interview.These questions will not only help you with the interview but would also
help understand MVC better.

Potrebbero piacerti anche