You are on page 1of 14

Overview

CLR Execution Model: Conceptual


Compilation Source Code Language Compiler
Code (IL)

Assembly
Metadata

Native Code Execution

JIT Compiler

Before installation or the first time each method is called

Generating and Running the Page Class Code


y An ASP.NET page runs as a unit, combining the server-side

elements in a page, such as controls, with the event-handling code you have written. If you use a Web site project, you do not have to precompile pages into assemblies. ASP.NET dynamically compiles pages and runs them the first time they are requested by a user. If there are any changes to the page or resources the page depends on, the page is automatically recompiled. y Web site projects also support precompilation of a Web project to enhance performance (for the first time a page request is received) and perform error checking as well as to support site deployment. y ASP.NET Web application projects must be explicitly compiled before they are deployed. The class or classes that the compiler creates depends on whether the page uses the single-file model or the code-behind model.

Single-File Pages
y In a Web site project, you can create single-file pages. In a single-file page, the

markup, server-side elements, and event-handling code are all in a single .aspx file. When the page is compiled, the ASP.NET generates and compiles a new class that derives from the base Page class or from a custom base class defined with the Inherits attribute of the @ Page directive. For example, if you create a new ASP.NET Web page named SamplePage1 in your application's root directory, a class named ASP.SamplePage1_aspx is generated that derives from the Page class. For pages in application subfolders, the subfolder name is used as part of the generated class. The generated class contains declarations for the controls in the .aspx page and contains your event handlers and other custom code. y The generated class is compiled into an assembly, and when the page is requested, the assembly is loaded into the application domain, and then the page class is instantiated and executed to render output to the browser. y For a Web site project, if you make changes to the page that would affect the generated classwhether by adding controls or modifying your codethe compiled class code is invalidated and a new class is generated.

y Advantages of Single-File Pages y As a rule, the single-file model is suitable for pages in which the code consists y y

y y

primarily of event handlers for the controls on the page. Advantages of the single-file page model include the following: In pages where there is not very much code, the convenience of keeping the code and markup in the same file can outweigh other advantages of the codebehind model. For example, it can be easier to study a single-file page because you can see the code and the markup in one place. Pages written using the single-file model are slightly easier to deploy or to send to another programmer because there is only one file. Because there is no dependency between files, a single-file page is easier to rename if you are using tools other than Visual Studio. (If you use Visual Studio to rename a file, Visual Studio automatically renames both files.) Managing files in a source code control system is slightly easier, because the page is self-contained in a single file.

Code-Behind Pages
y Code-behind pages are the default in Web application projects and

are optional in Web site projects. y In the code-behind model, the page's markup and server-side elements, including control declarations, are in an .aspx file, while your page code is in a separate code file. y The code file contains a partial classthat is, a class declaration with the keyword partial (Partial in Visual Basic) indicating that it contains only some of the total code that makes up the full class for the page. y In the partial class, you add the code that your application requires for the page. This typically consists of event handlers, but can include any methods or properties that you need.

Code behind pages contd.


y The inheritance model for code-behind pages is slightly more complex than that y

y y

for single-file pages. The model is this: The code-behind file contains a partial class that inherits from a base page class. The base page class can be the Page class, or it can be another class that derives from Page. The .aspx file contains an Inherits attribute in the @ Page directive that points to the code-behind partial class. When the page is compiled, ASP.NET generates a partial class based on the .aspx file; this class is a partial class of the code-behind class file. The generated partial class file contains declarations for the page's controls. This partial class enables your code-behind file to be used as part of a complete class without requiring you to declare the controls explicitly. Finally, ASP.NET generates another class that inherits from the class generated in Step 3. This second generated class contains the code required to build the page. The second generated class and the code-behind class are compiled into an assembly that runs to render output to the browser.

y Advantages of Code-Behind Pages y Code-behind pages offer advantages that make them suitable

y y

y y

for Web applications with significant code or in which multiple developers are creating a Web site. Advantages of the code-behind model include the following: Code-behind pages offer a clean separation of the markup (user interface) and code. It is practical to have a designer working on the markup while a programmer writes code. Code is not exposed to page designers or others who are working only with the page markup. Code can be reused for multiple pages.

Directives

Interface An interface is like a class but all the methods and properties are abstract. An Interface cannot be instantiated like abstract class. All the methods and properties defined in Interface are by default public and abstract. Interface generally refers to an abstraction that an entity provides of itself to the outside. Interface can help in separating the methods for external and internal communication without effecting in the way external entities interact with the type..

Abstract Class An abstract class is a class with at least one method defined as abstract. This type of class cannot be instantiated. An abstract class can have one or more abstract methods and properties and other methods and properties like normal classes. y Partial Class A class defined in two or more files is called a partial class. The keyword partial is used to define the class. When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously. During compile time all the partial class are compiled into one type only.
y y

Sealed Class A sealed class is a class that cannot be inherited. Sealed classes are used to restrict the inheritance feature of object oriented programming.

Public, Private and Protected


y In C#, you can declare both variables and class methods as public, private or

protected. A public method is accessible from other classes and a private method is accessible only inside that class. Usually, you make all class variables private and write get and set accessor functions toset or obtain their values. y It is generally a bad idea to allow variables inside a class to be accessed directly from outside the class, since this violates the principle of encapsulation. In other words, the class is the only place where the actual data representation should be known, and you should be able to change the algorithms inside a class without anyone outside the class being any the wiser. y C# introduces the protected keyword as well. Both variables and methods can be protected. Protected variables can be accessed within the class and from any subclasses you derive from it. Similarly, protected methods are only accessible from that class and its derived classes. They are not publicly accessible from outside the class. If you do not declare any level of accessibility, private accessibility is assumed.

You might also like