15843 overview mix

14
Overview

Upload: anmol-bhasin

Post on 06-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 15843 Overview Mix

8/2/2019 15843 Overview Mix

http://slidepdf.com/reader/full/15843-overview-mix 1/14

Overview

Page 2: 15843 Overview Mix

8/2/2019 15843 Overview Mix

http://slidepdf.com/reader/full/15843-overview-mix 2/14

Page 3: 15843 Overview Mix

8/2/2019 15843 Overview Mix

http://slidepdf.com/reader/full/15843-overview-mix 3/14

AssemblyAssembly

CLR Execution Model: Conceptual

SourceSource

CodeCode

LanguageLanguage

Compiler Compiler 

CompilationCompilation

Before installation or Before installation or 

the first time eachthe first time eachmethod is called method is called 

ExecutionExecution

JIT JIT Compiler Compiler 

NativeNativeCodeCode

Code (IL)Code (IL)

 Metadata Metadata

Page 4: 15843 Overview Mix

8/2/2019 15843 Overview Mix

http://slidepdf.com/reader/full/15843-overview-mix 4/14

Generating  and Running  the Page 

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

elements in a page, such as controls, with the event-handling codeyou have written. If you use a Web site project, you do not have toprecompile pages into assemblies. ASP.NET dynamically compilespages and runs them the first time they are requested by a user. If 

there are any changes to the page or resources the page dependson, 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 compilercreates depends on whether the page uses the single-file model orthe code-behind model.

Page 5: 15843 Overview Mix

8/2/2019 15843 Overview Mix

http://slidepdf.com/reader/full/15843-overview-mix 5/14

Single-File Pages

y In a Web site project, you can create single-file pages. In a single-file page, themarkup, server-side elements, and event-handling code are all in a single .aspxfile. When the page is compiled, the ASP.NET generates and compiles a newclass that derives from the base Page class or from a custom base class definedwith the Inherits attribute of the @ Page directive. For example, if you create anew ASP.NET Web page named SamplePage1 in your application's root

directory, a class named ASP.SamplePage1_aspx is generated that derives fromthe Page class. For pages in application subfolders, the subfolder name is used aspart of the generated class. The generated class contains declarations for thecontrols in the .aspx page and contains your event handlers and other customcode.

y The generated class is compiled into an assembly, and when the page isrequested, the assembly is loaded into the application domain, and then the pageclass 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 thegenerated class³whether by adding controls or modifying your code³thecompiled class code is invalidated and a new class is generated.

Page 6: 15843 Overview Mix

8/2/2019 15843 Overview Mix

http://slidepdf.com/reader/full/15843-overview-mix 6/14

Page 7: 15843 Overview Mix

8/2/2019 15843 Overview Mix

http://slidepdf.com/reader/full/15843-overview-mix 7/14

y Advantages of Single-File Pages

y As a rule, the single-file model is suitable for pages in which the code consistsprimarily of event handlers for the controls on the page.

y Advantages of the single-file page model include the following:

y 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 code- behind model. For example, it can be easier to study a single-file page becauseyou can see the code and the markup in one place.

y Pages written using the single-file model are slightly easier to deploy or to sendto another programmer because there is only one file.

y 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 Studioto rename a file, Visual Studio automatically renames both files.)

y Managing files in a source code control system is slightly easier, because thepage is self-contained in a single file.

Page 8: 15843 Overview Mix

8/2/2019 15843 Overview Mix

http://slidepdf.com/reader/full/15843-overview-mix 8/14

Code-Behind Pages

y Code-behind pages are the default in Web application projects andare optional in Web site projects.

y In the code-behind model, the page's markup and server-sideelements, including control declarations, are in an .aspx file, whileyour page code is in a separate code file.

y The code file contains a partial class³that is, a class declarationwith the keyword partial (Partial in Visual Basic) indicating that itcontains only some of the total code that makes up the full classfor the page.

yIn the partial class, you add the code that your application requiresfor the page. This typically consists of event handlers, but caninclude any methods or properties that you need.

Page 9: 15843 Overview Mix

8/2/2019 15843 Overview Mix

http://slidepdf.com/reader/full/15843-overview-mix 9/14

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

for single-file pages. The model is this:

y 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 derivesfrom Page.

y

The .aspx file contains an Inherits attribute in the @ Page directive that pointsto the code-behind partial class.

y 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 generatedpartial class file contains declarations for the page's controls. This partial classenables your code-behind file to be used as part of a complete class withoutrequiring you to declare the controls explicitly.

y Finally, ASP.NET generates another class that inherits from the class generatedin Step 3. This second generated class contains the code required to build thepage. The second generated class and the code-behind class are compiled into anassembly that runs to render output to the browser.

Page 10: 15843 Overview Mix

8/2/2019 15843 Overview Mix

http://slidepdf.com/reader/full/15843-overview-mix 10/14

y Advantages of Code-Behind Pages

y Code-behind pages offer advantages that make them suitablefor Web applications with significant code or in whichmultiple developers are creating a Web site.

y Advantages of the code-behind model include the following:

y Code-behind pages offer a clean separation of the markup(user interface) and code. It is practical to have a designerworking on the markup while a programmer writes code.

y Code is not exposed to page designers or others who areworking only with the page markup.

y Code can be reused for multiple pages.

Page 11: 15843 Overview Mix

8/2/2019 15843 Overview Mix

http://slidepdf.com/reader/full/15843-overview-mix 11/14

Page 12: 15843 Overview Mix

8/2/2019 15843 Overview Mix

http://slidepdf.com/reader/full/15843-overview-mix 12/14

Directives

Page 13: 15843 Overview Mix

8/2/2019 15843 Overview Mix

http://slidepdf.com/reader/full/15843-overview-mix 13/14

y Interface

An interface is like a class but all the methods and properties are abstract. An Interface cannot beinstantiated like abstract class. All the methods and properties defined in Interface are by defaultpublic and abstract.Interface generally refers to an abstraction that an entity provides of itself to the outside. Interfacecan help in separating the methods for external and internal communication without effecting in

the way external entities interact with the type..

y Abstract Class

An abstract class is a class with at least one method defined as abstract. This type of class cannot beinstantiated. An abstract class can have one or more abstract methods and properties and othermethods 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 definethe class. When working on large projects, spreading a class over separate files allows multipleprogrammers to work on it simultaneously. During compile time all the partial class are compiledinto one type only.

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.

Page 14: 15843 Overview Mix

8/2/2019 15843 Overview Mix

http://slidepdf.com/reader/full/15843-overview-mix 14/14

Public, Private and Protected

y In C#, you can declare both variables and class methods as public, private orprotected. A public method is accessible from other classes and a privatemethod is accessible only inside that class. Usually, you make all class variablesprivate 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 directlyfrom outside the class, since this violates the principle of encapsulation.In otherwords, the class is the only place where the actual data representation should beknown, and you should be able to change the algorithms inside a class withoutanyone outside the class being any the wiser.

y C# introduces the protected keyword as well. Both variables and methods can beprotected. Protected variables can be accessed within the class and from any

subclasses you derive from it. Similarly, protected methods are only accessiblefrom that class and its derived classes. They are not publicly accessible fromoutside the class. If you do not declare any level of accessibility, privateaccessibility is assumed.