Private Constructor: Public Class Private Public Static Int Public Static Int Return
Private Constructor: Public Class Private Public Static Int Public Static Int Return
class TestCounter
{
static void Main()
{
// If you uncomment the following statement, it will generate
// an error because the constructor is inaccessible:
// Counter aCounter = new Counter(); // Error
Counter.currentCount = 100;
Counter.IncrementCount();
System.Console.WriteLine("New count: {0}", Counter.currentCount);
}
}
Static Constructor
Initialization of all static members in the class we use static constructor.
When we need to assign value of static variable in class, we need to use static constructor.
When we create a class as static then it must have all the members as static.
HTTP Handlers
The Http Handler and Http Module are programs which are used by ASP.NET to handle requests based
on extensions.
Whenever the IIS Server receives a request, it looks for an ISAPI filter that is capable of handling web
requests. In ASP.NET, this is done by aspnet_isapi.dll. Same kind of process happens when an ASP.NET
page is triggered. It looks for Http Handler in the web.config files for any request setting.
The most common handler is an ASP.NET page handler that processes .aspx files. When users request an
.aspx file, the request is processed by the page handler
HTTP Modules is an assembly that is called on every request that is made to your application
and is used if we want to have our own functionality working along with the default ASP.NET
2
functionality. There is one Handler for a specific request but there could be N number of modules for
that.
We will be using the built-in extensions (.ashx) that are already mapped to ASP.NET so that we can avoid
the necessity to modify the IIS extensions mapping. With .ashx extension, there is no requirement for
registration in the web/machine.config.
What is an Index
Index is a database object, which can be created on one or more columns (16 Max column
combinations). The index will improve the performance of data retrieval and adds some
overhead on data modification such as create, delete and modify.
In its simplest definition a clustered index is an index that stores the actual data and a non-clustered
index is just a pointer to the data.
A clustered index is a special type of index in which the logical order of the index match the physical
stored order of the rows on disk
Composite Indexes:
A composite index is an index on two or more columns of a table.
LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table.
RIGHT JOIN: returns all rows from the right table, even if there are no matches in the left table.
FULL JOIN: returns rows when there is a match in one of the tables.
SELF JOIN: is used to join a table to itself, as if the table were two tables, temporarily renaming at least one
table in the SQL statement.
CARTESIAN JOIN: returns the cartesian product of the sets of records from the two or more joined tables.
2
3
The SQL UNION clause/operator is used to combine the results of two or more SELECT statements without returning
any duplicate rows.
To use UNION, each SELECT must have the same number of columns selected, the same number of column
expressions, the same data type, and have them in the same order but they do not have to be the same length.
GARBAGE COLLECTION
The .NET Framework's garbage collector manages the allocation and release of memory for your
application. Each time you create a new object, the common language runtime allocates memory for the
object from the managed heap. As long as address space is available in the managed heap, the runtime
continues to allocate space for new objects. However, memory is not infinite. Eventually the garbage
collector must perform a collection in order to free some memory.
3
4
Serialization
The serialization is the process of converting the objects into stream of bytes. They are used for
transport the objects (via remoting) and persist objects (via files and databases) When developing
smaller applications that do not have a database (or other formal storage mechanism) or data that
doesn't need to be stored in a database (such as the state of a web application), you often still would
like to save the data for later retrieval. This easy way is called serialization. Serialization is the process of
storing an object, including all of its public and private fields, to a stream. Deserialization is the opposite
– restoring an object's field values from a stream.
Serialization is a process of taking an object and converting into a form so that it can be
transported across the network or can be persisted in the storage location. This storage
location can be physical file, database or ASP.NET Cache.
Types of Binding
BasicHttpBinding: Basic web service communication. Exposes WCF services as legacy ASMX web
services. Used for interoperability. No security by default.
WSHttpBinding: Web services with WS-* support. Supports transactions and reliable messaging.
WSDualHttpBinding: Web services with duplex contract and transaction support.
WSFederationHttpBinding: Web services with federated security. Supports transactions.
MsmqIntegrationBinding: Communication directly with MSMQ applications. Supports transactions.
NetMsmqBinding: Communication between WCF applications by using queuing. Supports
transactions.
NetNamedPipeBinding: Communication between WCF applications on same computer. Supports
duplex contracts and transactions.
4
5
Web services can only be invoked by HTTP. While Services or a WCF component can be invoked by
any protocol and any transport type. Second web services are not flexible. However Services are
flexible. If you make a new version of the service then you need to just expose a new end. Web
service use XML Serializer while WCF use DataContractSerializer.It is new WCF serializer.
DataContractSerializer translate the .NET framework objects into XML and vice-versa.
By default WCF uses DataContractSerializer.
WCF bind an incoming message request to a particular service instance, so the available
modes are:
Per Call: instance created for each call, most efficient in term of memory but need to maintain
session.
Per Session: Instance created for a complete session of a user. Session is maintained.
Single: Only one instance created for all clients/users and shared among all. Least efficient in terms
of memory.
Transport Security: Transport level security is the easiest to implement. WCF uses transport protocols
like TCP, HTTP, MSMQ etc and every of these protocols have their own security mechanisms. One of
the common implementation of transport level security is HTTPS. HTTPS is implemented over HTTP
protocols with SSL providing the security mechanism. No coding change is required it’s more of
using the existing security mechanism provided by the protocol.
Message Security: Message level security is implemented with message data itself. Some of the
common ways of implementing message level security is by encrypting data using some standard
encryption algorithm.
5
6
Contracts in WCF:
The contract is a standard way of describing what the service does.
Types of Contracts:
1. Service Contract
2. Data Contract
3. Message Contract
4. Fault Contract
Message Contract
Message is the packet of data which contains important information. WCF uses these messages to
transfer information from Source to destination.
WCF uses SOAP (Simple Object Access Protocol) Message format for communication. SOAP message
contain Envelope, Header and Body. SOAP envelope contains name, namespace, header and body
element. SOAP Header contains important information which is not directly related to message.
SOAP body contains information which is used by the target.
Message Pattern
[MessageContract]
public class EmployeeDetails
{
[MessageHeader]
public string EmpID;
[MessageBodyMember]
public string Name;
[MessageBodyMember]
}
6
7
When I use this EmployeeDeatils type in the service operation as parameter. WCF will add extra
header call 'EmpID' to the SOAP envelope. It also adds Name, Designation, Salary, Location as extra
member to the SOAP Body.
Fault Contract
Suppose the service I am consumed is not working in the client application. I want to know the real
cause of the problem. How I can know the error? For this we are having Fault Contract. Fault Contract
provides documented view for error occurred in the service to client. This helps us to easy identity,
what error has occurred.
Entity Framework
Entity Framework is an Object Relational Mapper (ORM). It basically generates business objects and
entities according to the database tables and provides the mechanism for:
Event Bubbling
The ASP.NET page framework provides a technique called event bubbling that allows a child control
to propagate events up its containment hierarchy. Event bubbling enables events to be raised from a
more convenient location in the controls hierarchy and allows event handlers to be attached to the
original control as well as to the control that exposes the bubbled event.
Event bubbling is used by the data-bound controls (Repeater, DataList, and DataGrid) to expose
command events raised by child controls (within item templates) as top-level events.
Lambda Expression
A lambda expression is an anonymous function and it is mostly used to create delegates in LINQ.
It's shorthand that allows you to write a method without the name.
Benefits:
1. It allows you to write a method in the same place you are going to use it.
7
8
2. It is useful in places where a method is being used only once, and the method definition is short.
3. It saves you the effort of declaring and writing a separate method to the containing class.
Procedure can return zero or n values whereas function can return one value which is mandatory.
Procedures can have input/output parameters for it whereas functions can have only input
parameters.
Procedure allows select as well as DML statement in it whereas function allows only select statement
in it.
Functions can be called from procedure whereas procedures cannot be called from function.
Exception can be handled by try-catch block in a procedure whereas try-catch block cannot be used
in a function.
Procedures cannot be utilized in a select statement whereas function can be embedded in a select
statement.
Procedure can change or alter database but the function cannot.
Dot NET assemblies may or may not be executable, i.e., they might exist as the executable
(.exe) file or dynamic link library (DLL) file. All the .NET assemblies contain the definition of
types, versioning information for the type, meta-data, and manifest.
Shared assemblies (also called strong named assemblies) are copied to a single location
(usually the Global assembly cache). Hence, shared assemblies are not copied in the private
folders of each calling assembly. Each shared assembly has a four part name including its
face name, version, public key token and culture information. The public key token and
version information makes it almost impossible for two different assemblies with the same
name or for two similar assemblies with different version to mix with each other.
An assembly can be a single file or it may consist of the multiple files. Assembly is one of
the most interesting and extremely useful areas of .NET architecture along with reflections
and attributes.
GAC
8
9
Each computer where the common language runtime is installed has a machine-wide code
cache called the global assembly cache. The global assembly cache stores assemblies
specifically designated to be shared by several applications on the computer.
What is difference between server side control and client side control in asp.net?
Server-side controls like ASP:Calendar, ASP:Textbox and any other ASP-prefixed control execute on ASP
platform, on the Server and have attribute Runat=”Server”.
Client-side control are the typical HTML textboxes, textareas, etc. because those controls are rendered
(and its Javascript code is executed) in the user's browser. Client Side controls are not "directly"
accessible on code behind. You can access them with other methods.
Server Side Controls can be directly accessible from code behind.
SQL Server: Session data is store in the configured sql server database
aspnet_regsql -S serverName -U UserName -P Password -ssadd -sstype p
<sessionstate mode="SQLServer" timeout="20"
allowcustomsqldatabase="true" sqlconnectionstring="Data
Source=Server;User ID=UserID;Password=Password;" cookieless="false">
State Server: Session data is sent to the configured state server service
9
10
Server Side
Application state
Session state
View State: When the page is processed, the current state of the page and controls is hashed into
a string and saved in the page as a hidden field, if the amount of data stored in the ViewState
property exceeds the specified value in the MaxPageStateFieldLength property. When the page
is posted back to the server, the page parses the view-state string at page initialization and
restores property information in the page.
10
11
Types of authentication
ASP.NET actually supports four types of authentication:
1.Windows authentication
2.Forms authentication
3.Passport authentication
4.Anonymous access
1.Windows authentication.
If our application is targeted for use inside an organization, and users accessing the application
have existing user accounts within the local user database of the Web server or Active Directory,
you should authenticate users with Windows authentication.
2. Forms authentication
by default, Form authentication is used.
Form-based authentication presents the user with an HTML-based Web page that prompts the
user for credentials.
3. Passport authentication
you can also authenticate users using a service from Microsoft called Passport. Passport
is a centralized directory of user information that Web sites can use, in exchange for a fee, to
authenticate users.
4. Anonymous access
it will be used only by anonymous users.
<authentication mode="None" />
Can we have 2 methods with the same name and parameter but different return type?
Within same class it is not possible and will give an error, but one virtual function can be in base class
and we can override it or use "new" keyword in derived class with different return type.
How many cluster and non cluster index can be used in SQL?
For SQL Server 2008: 1 Clustered Index + 999 Non clustered Index = 1000 Index
11
12
Validate
Event
Pre-render
Save view state
Render
Unload
At which event we can change the master page
You can change the master page dynamically @ Pre Init event
If you anticipate creating multiple versions of your component, create an abstract class.
Abstract classes provide a simple and easy way to version your components. By updating the
base class, all inheriting classes are automatically updated with the change. Interfaces, on the
other hand, cannot be changed once created. If a new version of an interface is required, you
must create a whole new interface.
If the functionality you are creating will be useful across a wide range of disparate objects,
use an interface. Abstract classes should be used primarily for objects that are closely related,
whereas interfaces are best suited for providing common functionality to unrelated classes.
If you are designing small, concise bits of functionality, use interfaces. If you are designing
large functional units, use an abstract class.
12
13
13
14
Shadowing in C#
The out and the ref parameters are used to return values in the same variables, that you pass an an
argument of a method. These both parameters are very useful when your method needs to return more
than one values.
HttpContext
ASP.NET HttpContext.Current.Items allows us to hold information for a single request
14
15
IEnumerable<employee> emp =
dc.Employees.Where(x => x.Desc.StartsWith("soft"));
emp = emp.Take(1);
IQueryable<employee> emplist =
dc.Employees.Where(x => x.Desc.StartsWith("soft"));
emplist = emplist.Take(1);
Why can't you use the keyword 'this' in a static method in .Net?
"this" represents an instance of the class at runtime, so this can't be used in a static context because it
won't be referencing any instance.
1. We have two classes Base Class and child Class, Child Class inherit base class. If we make the
object of child class then which class constructor called first?
Ans: Base class constructor will be call first.
2. Can we declare an Abstract method in non-abstract class?
Ans: No
3. Can we give the return type of constructor?
Ans: No
Extension Methods: This is a new feature in C# 3.0. This method allows us to add a new static method
to the existing classes.
Partial Class: A partial class allows a single class to be divided into two separate physical files. During
compile time, these files get compiled into a single class. For instance, you can see in the below figure
we have the customer class divided into two different files “customer1.cs” and “customer2.cs”.
Using keyword in C#: It will clean up any unmanaged resources. All variables you use in Using Block get
disposed when you exit the Using Block. If you declare any un-managed object in using block, you don't
need to explicitly dispose those objects. Once the block execution is over, the Dispose will be called
automatically.
15