Professional Documents
Culture Documents
OOPs
OOPs
2. OOP helps to maintain the code with DRY ("Don't Repeat Yourself") principle,
and makes the code easier to maintain, modify and debug.
3. Basically, Don't Repeat Yourself" (DRY) principle is about reducing the repetition
of code. Means common for the application, and place them
Class -
1. Class is a user-defined data type, which holds its own data members and
member functions, which can be accessed and used by creating an instance of that
class. The variables inside class definition are called data members and the
functions are called member functions.
Object -
object in memory will contain the information about the methods, variables,
and behaviour of that class.
Encapsulation –
Abstraction –
1. Abstraction is a process where you show only “relevant” data and “hide”
unnecessary details of an object from the user.
2. It’s a process of hiding the implementation details and displaying the essential
features.
Polymorphism –
1. Creating multiple methods in a class with the same name but with different
parameters and different types is called method overloading.
override a same method in derived class with same signature and same type.
Access Specifiers –
2. Private can be accessible inside the class only through member functions.
3. Protected can be just like private but accessible in derived classes also through
member functions.
5. Protected Internal can be visible inside the assembly through objects and in
derived classes outside the assembly through member functions.
Constructor –
2. The main use of constructors is to initialize private fields of the class while
creating an instance for the class.
3. When you have not created a constructor in the class, the compiler will
automatically create a default constructor in the class.
4. The default constructor initializes all numeric fields in the class to zero and all
string and object fields to null.
5. A class can have any number of constructors but not of same type.
6. A constructor does not have any return type, not even void.
• Default Constructor
• Parameterized Constructor
• Copy Constructor
• Static Constructor
• Private Constructor
Private Constructor –
• Private constructor is a special instance constructor which is used in a class that
contains static member only.
• If a class has one or more private constructor and no public constructor then
other classes is not allowed to create instance of this class this mean
you can neither create the object of the class nor it can be inherit by other class.
• The main purpose of creating private constructor is to restrict the class from
being instantiated when it contains every member as static.
Note - No, object of a class having private constructor can not be instantiated from outside of
the class.
Static constructor –
• Static constructor is a special constructor that gets called before the first object
of the class is created.
• It is used to initialize any static data, or to perform a particular action that needs
to be performed once only.
Static Class -
It is the type of class that cannot be instantiated, in other words, we cannot create an
object of that class using the new keyword and the
The static member is callable on a class even when no instance of the class has been
created. The static member is always accessed by the class name,
Copy Constructor in C#
The constructor which creates an object by copying variables from another object is
called a copy constructor.
name=emp.name;
age=emp.age;
Interface -
3. An Interface is an abstract class which has only public abstract methods, and the
methods only have the declaration and not the definition.
4. Interfaces have all the methods having only declaration but no definition
5. In an interface class, all the methods are public.
Abstract Class -
5. Abstract classes are declared using the abstract keyword. Objects cannot be
created for abstract classes. If you want to use it then it must be
10. The abstract keyword can be used with class, methods, properties, indexers and
events.
12. A concrete class can’t inherit more than one abstract class, in other words
multiple Inheritance is not possible.
• Hence, to restrict the calling of abstract methods, the compiler does not allow
you to instantiate an abstract class.
You can define all static and non-static members including properties, fields, indexers
and also abstract methods.
You can define all static and non-static members including properties, fields, indexers
and also abstract methods.
• A class can implement any number of interfaces but a subclass can at most use
only one abstract class.
• In an abstract class, all data members or functions are private by default while in
an interface all are public, we can’t change them manually.
• In an abstract class, we need to use abstract keywords to declare abstract
methods, while in an interface we don’t need to use that.
• An abstract class can’t be used for multiple inheritance while the interface can
be used as multiple inheritance.
• An abstract class use constructor while in an interface we don’t have any type of
constructor.
Collection -
it grows automatically.
Stack Stack stores the values in LIFO style (Last In First Out). It
provides a Push() method to add a value and Pop() & Peek() methods to retrieve
Queue Queue stores the values in FIFO style (First In First Out). It keeps
the order in which the values were added. It provides an Enqueue() method
Hashtable Hashtable stores key and value pairs. It retrieves the values by
comparing the hash value of the keys.
Array list
List (Generic)
Hash table
Dictionary
Stack
Queue
Generics - A generic collection is strongly typed (you can store one type of objects into it) so
that we can eliminate runtime type mismatches,
Dictionary
Dictionary class is a strong type < TKey,TValue > Hence, you must specify the data types
for key and value.
When you try to access non existing key dictionary, it gives runtime error.
Hashtable
When you try to access non existing key Hashtable, it gives null values.
1. Constant is a variable where declaration and initialization at the same time. When
value is constant at compile time and can not change a value
.NET provides a special type called Delegate which acts as an intermediary between the
sender object and receiver object
No, Multiple catch blocks can't be executed. Once the proper catch code executed, the
control is transferred to the finally block and then the code that
Static declared variables or methods are globally accessible without creating an instance
of the class. The compiler stores the address of the method
as the entry point and uses this information to begin execution before any objects are
created.
And Void is a type modifier that states that the method or variable does not return any
value.
An argument passed as ref must be initialized before passing to the method whereas
out parameter needs not to be initialized before passing to a method
We can't use ‘This' in a static method because we can only use static variables/methods
in a static method.
Value types are stored in the Stack whereas reference types stored on heap.
Value types: int, enum , byte, decimal, double, float, long
The Clone() method returns a new array (a shallow copy) object containing all the
elements in the original array.
The CopyTo() method copies the elements into another existing array.
A shallow copy means the contents (each array element) contains references to the
same object as the elements in the original array.
A deep copy (which neither of these methods performs) would create a new instance of
each element's object, resulting in a different, yet identical object.
Properties in C# -
• It uses methods to access and assign values to private fields called accessors.
• The Get and Set portions or blocks of a property are called accessors.
String - System.String is immutable. When we modify the value of a string variable, then
a new memory is allocated to the new value and the previous memory
allocation released.
StringBuilder - System.StringBuilder was designed to have a concept of a mutable string
where a variety of operations can be performed without allocation
"is" operator is used to check the compatibility of an object with a given type, and it
returns the result as Boolean.
In a singleton pattern, a class can only have one instance and provides an access point
to it globally.
Eg:
There are various ways to implement a singleton pattern in C#. The following are the
common characteristics of a singleton pattern.
Sealed class.
A public and static way of getting the reference to the created instance.
Dependency Injection in c# -
1. .NET supports the dependency injection (DI) software design pattern, which is a
technique for achieving Inversion of Control (IoC) between classes
tight coupling among software components. Dependency Injection reduces the hard-
coded dependencies among your classes by injecting those dependencies at
Type of Injection -
1. Constructor Injection.
2. Property Injection.
3. Method Injection.
Extenision Method -
Extension methods enable you to add methods to existing types without creating a new
derived type, recompiling, or otherwise modifying the original type.
An extension method is a static method of a static class, where the "this" modifier is
applied to the first parameter. The type of the first parameter
1. Delegates allow to create an object which object have the reference of any
parameter.
4. They have a signature and a return type. A function that is added to delegates
must be compatible with this signature.
6. Once a delegate object has been created, it may dynamically invoke the
methods it points to at runtime.
Anonymous Type –
Anonymous types allow us to create new types without defining them. This is a way of
defining read-only properties in a single object without having to
We can create anonymous types by using “new” keyword together with the object
initializer.
Example
2. {
3. ForeName = "Jignesh",
4. SurName = "Trivedi"
5. };
Web API
It in an open source framework for writing HTTP APIs. These APIs can be RESTful or not.
Most HTTP APIs we write are not RESTful. This framework implements
HTTP protocol specification and hence you hear terms like URIs, request/response
headers, caching, versioning, various content types(formats).
Note: I have not used the term Web Services deliberately because it is a confusing term
to use. Some people use this as a generic concept, I preferred to
call them HTTP APIs. There is an actual framework named 'Web Services' by Microsoft
like Web API. However it implements another protocol called SOAP.
REST -
of the web.
It is used to develop WEB API which enable interaction between client and server.
RESTFULL -
is always safe.
The Authentication server sends an Access token to the client as a response. This token
contains enough data to identify a particular user and it has an
expiry time. The client application then uses the token to access the restricted resources
in the next requests until the token is valid. If the Access
token is expired, then the client application can request for a new access token by using
Refresh token.
Microsoft.Owin.Host.SystemWeb
Microsoft.Owin.Security.OAuth
Microsoft.Owin.Cors
Var vs Dynamic -
Var -
It is a statically typed.
Ex -
var a = 999;
Dynamic -
it is a dynamic type.
var a = 999;
a = "this is a string." we use dynamic instead of var, it will not only compile, but
will also work at run-time.
Dispose vs Finalize -
The dispose() method releases the unmanaged resources that are held by an object of the class.
The unmanaged resources are files, data connections, etc.
The method dispose() is declared in the interface IDisposeable and it is implemented by the
class by implementing the interface IDisposable.
The programmer has to implement it manually when you are creating a custom class that will be
used by others.
the method finalize() must not be accessible from outside the class, and it must only be
accessible to the garbage collector.
?????
IEnumerable vs IQuerable -
IEnumerable
IEnumerable is suitable for querying data from in-memory collections like List, Array and
so on.
While querying data from the database, IEnumerable executes "select query" on the
server-side, loads data in-memory on the client-side and then filters the data.
IQueryable
IQueryable is suitable for querying data from out-memory (like remote database,
service) collections.
While querying data from a database, IQueryable executes a "select query" on server-
side with all filters.
SOLID Principles -
SOLID principles are the design principles that enable us to manage most of the
software design problems.
S: Single Responsibility Principle (SRP) - SRP says "Every software module should have
only one reason to change".
This means that every class, or similar structure, in your code should have only
one job to do.
Everything in that class should be related to a single purpose. Our class should
not be like a Swiss knife wherein if one of them needs to be changed
O: Open closed Principle (OCP) - The Open/closed Principle says "A software
module/class is open for extension and closed for modification".
L: Liskov substitution Principle (LSP) - "you should be able to use any derived class
instead of a parent class and have it behave in the same manner without
modification.
It ensures that a derived class does not affect the behavior of the parent class,
in other words,, that a derived class must be substitutable for its base class.
I: Interface Segregation Principle (ISP) - Its states that clients should not be forced to
implement interfaces they don't use.
D: Dependency Inversion Principle (DIP) - Dependency Inversion Principle (DIP) states
that high-level modules/classes should not depend on low-level
IHttpActionResult
HttpResponseMessage
Void
A: It is the process that determines the action and controller that should be called.
You have to install the CORS nuget package by using Package Manager Console to
enable CORS in Web API.
add config.EnableCors();
Add EnableCors attribute to the Controller class and define the origin.
Authentication
Authorization
Maintaining Session
What are the differences between HTTP Get and HTTP Post?
Parameters of GET are included in the URL; while parameters of POST are included in
the body
GET requests do not make any changes to the server; while POST does make changes to
the server
Using HttpResponseException –This exception class helps to return the HTTP status code
specified in the exception Constructor.
Using HttpError – This exception class helps to return meaningful error code to the
client as HttpResponseMessage.
A: Basic Authentication in ASP.Net Web API can be implemented where the client sends
a request with an Authorization header and word Basic.
What is REST?
A: REST stands for Representational State Transfer. This is an architectural pattern that helps in
exchanging data and treats all the services as resources
and a client can access these resources by using HTTP protocol methods which include PUT, GET,
POST, and DELETE.
What are the differences between ASP.NET MVC and ASP.NET Web API?
A: MVC is used to create web applications that can return views as well as data; while ASP.NET
Web API is used to create restful HTTP services simply, which returns
A: These are classes that are responsible for response data. The Web API understands the
request data format as well as sends data in the format as expected
by the clients
config.MapHttpAttributeRoutes();
A: System.Web.Http.Results namespace
You can use the following code to register an exception filter globally –
GlobalConfiguration.Configuration.Filters.Add (new
MyTestCustomerStore.NotImplExceptionFilterAttribute());
What is Program.cs ?
Program.cs is where the application starts. Program.cs class file is entry point of our application
and creates an instance of IWebHost which hosts a web application.
Content Negotiation is a mechanism that allows a user to decide what kind of response he want
to get from the API.
More Explanation:
Suppose, a developer is friendly with JSON and he wants that all of the API response should be
in JSON format, then he just need to send an "Accept" header with "application/json" value. Web API
will read this header and will provide all the response in JSON format.
The same thing applies for XML as well. To get all response in XML format, developer need to
pass "application/xml" in "Accept" header of API request.
Content Negotiation is a mechanism that allows a user to decide what kind of response he want
to get from the API.
Client should have the ability to decide in which format they want the response – whether they
want the response in XML or JSON etc.
There are two main headers which hold the responsibility of content negotiation
1. Content-Type
2. Accept
if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Found);
as shown below.
config.MapHttpAttributeRoutes();
Difference between "throw" and "throw ex" in C#
throw - Its gives original error stack information with empty parameter
https://stackoverflow.com/questions/31089221/what-is-the-difference-between-put-post-and-
patch
Task -
A Task represents some asynchronous operation and is part of the Task Parallel Library,
a set of APIs for running tasks asynchronously and in parallel.
Thread -
html 5 tags