Object-Oriented Programming and ASP.NET Development
1. Features of OOP
Encapsulation
Data hiding by wrapping variables and methods within a class. Example: Private members accessible only via public methods.
Abstraction
Simplifies complex systems by providing necessary details and hiding the internal workings. Example: Interface or abstract classes in C#.
Inheritance
Reusing existing class properties and methods by deriving new classes. Example: class Car : Vehicle
Polymorphism
One interface, many implementations—achieved through method overriding and overloading. Example: Add(int a, int b)
and Add(double a, double b)
Reusability
Reusing code components via inheritance and composition, reducing redundancy.
2. OOP Concepts in C#
Encapsulation
Example in C#:
class Employee {
private string name;
public void SetName(string n) { name = n; }
public string GetName() { return name; }
}
Abstraction
Hide implementation using abstract classes and interfaces:
abstract class Shape {
public abstract void Draw();
}
Inheritance
Example:
class Dog : Animal {
public void Bark() { Console.WriteLine("Bark"); }
}
Polymorphism
Achieved through overriding and overloading:
public class Animal {
public virtual void Sound() { Console.WriteLine("Animal Sound"); }
}
public class Dog : Animal {
public override void Sound() { Console.WriteLine("Bark"); }
}
Interfaces
Enforce abstraction:
interface IVehicle {
void Start();
}
3. ASP.NET Page Life Cycle
- Page Request: The browser requests a page, triggering ASP.NET to create a request object.
- Start: ASP.NET checks if it’s a postback (return visit) or a new request.
- Initialization: Controls on the page are initialized, but their properties haven’t yet been restored from view state.
- Load: Controls are loaded with information. You can now manipulate them.
- Postback Event Handling: Handles events like button clicks.
- Rendering: ASP.NET generates the HTML output for the browser.
- Unload: Cleanup of used resources happens here.
Flowchart: Page Request → Start → Initialization → Load → Postback Events → Rendering → Unload
4. Collections in .NET
- ArrayList: Dynamically resizable array (not type-safe).
- List<T>: Generic list providing type safety and flexibility.
- Dictionary<TKey, TValue>: Stores key-value pairs for fast lookups.
- Queue<T>: First In First Out (FIFO) collection.
- Stack<T>: Last In First Out (LIFO) collection.
Example:
List<int> numbers = new List<int> { 1, 2, 3, 4 };
Dictionary<int, string> dict = new Dictionary<int, string> { {1, "One"}, {2, "Two"} };
5. Controls in C#
- Button: Generates a button that triggers events on click.
- Label: Displays static text on a form.
- TextBox: Allows user input.
- DropDownList: Allows selection from a list of options.
- GridView: Displays tabular data and supports editing.
Example:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit_Click" />
6. .NET Framework Architecture
- Common Language Runtime (CLR): Core runtime engine managing code execution, memory, and thread management.
- Base Class Library (BCL): Provides essential classes for IO, threading, collections, etc.
- Framework Class Library (FCL): More specialized libraries built on the BCL for UI, ADO.NET, etc.
- Assemblies: Packaging units for deployment (DLLs or EXEs).
- Languages Supported: C#, VB.NET, F# through CLS compliance.
7. CSS in ASP.NET
CSS can be added through inline styles, internal styles, or external style sheets.
- Inline Styles:
<div style="color:red">Text</div>
- Internal Styles: Styles defined inside
<style>
tags in the HTML<head>
. - External Stylesheets: Linked using
<link rel="stylesheet">
.
Example:
<head>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
8. SDI vs. MDI
SDI (Single Document Interface)
- Supports one window at a time.
- Simpler interface (e.g., Notepad).
- Lower memory and processing overhead.
- Typically used in simpler applications.
- Easier to implement.
MDI (Multiple Document Interface)
- Supports multiple child windows.
- Useful for complex applications (e.g., Excel).
- Higher memory usage.
- Child windows can be minimized, maximized.
- Complex to implement.
9. ASP.NET Server Controls
- HTML Controls: Basic HTML input controls used in ASP.NET (e.g.,
<input>
). - Web Controls: Abstracted, stateful server controls such as Button, Label.
- Validation Controls: Built-in validation like RequiredFieldValidator, RangeValidator.
- Data Controls: For displaying data, like GridView, Repeater.
- Navigation Controls: Menu, TreeView, SiteMapPath.
10. State Management in ASP.NET
- Session State: Stores user data per session.
- Application State: Stores global data accessible to all users.
- ViewState: Stores user data between postbacks in a page.
- Cookies: Store small pieces of data on the client side.
- web.config: Configuration settings for session, security, and application.
- global.asax: Application-level event handler file.
11. Navigation Controls in ASP.NET
- Menu: Provides a list of navigation items in hierarchical order.
- TreeView: Displays a tree structure of nodes (often used for navigation).
- SiteMapPath: Displays breadcrumb navigation on a web page.
- SiteMapDataSource: Provides data to navigation controls like Menu and TreeView.
- HyperLink: A link control for navigating between pages.
12. SQL in ASP.NET Pages
SQL Commands: Used to interact with databases.
- SELECT: Retrieves data.
- INSERT: Adds new data.
- UPDATE: Modifies existing data.
- DELETE: Removes data.
- Connection: Using SqlConnection to connect to a database.
- Command: Executes SQL commands.
- DataReader: Retrieves a read-only, forward-only stream of data.
- DataSet: Represents an in-memory cache of data.
13. ADO.NET Architecture
- Data Provider: Components such as SqlConnection, SqlCommand, DataAdapter, and DataReader.
- Connection: Manages the connection to the database.
- Command: Executes SQL queries or stored procedures.
- DataReader: Fetches a stream of data in forward-only mode.
- DataAdapter: Fills a DataSet for in-memory data manipulation.
14. LINQ Operators
- Where: Filters elements based on a condition.
- Select: Projects each element into a new form.
- GroupBy: Groups elements based on a key.
- OrderBy: Sorts elements in ascending or descending order.
- Join: Joins two collections based on matching keys.
15. LINQ Query Syntax in ASP.NET
Example:
var result = from s in students where s.Age > 20 select s;
16. ASP.NET Security
Authentication
Verifies user identity.
- Forms Authentication: Like login form-based authentication.
- Windows Authentication: Uses the Windows user account to authenticate.
- Passport Authentication: Centralized authentication service.
Authorization
Determines the access level of authenticated users.
- Role-based Authorization: Users are assigned roles to access resources.
- URL Authorization: Restricts access to particular URLs.
Impersonation
Allows the ASP.NET application to run under the context of a different user (usually for privilege elevation).
- Enabled using
<identity impersonate="true" />
in web.config.
17. Forms in C# (.NET)
Definition: A form in C# is a window or screen in desktop applications that provides an interface for user input.
Main Elements: Form consists of buttons, text boxes, labels, etc., for interaction.
Event-driven: Forms respond to events such as button clicks or text input.
Example Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{ MessageBox.Show("Button clicked!");
}
}
Event Handling: Action is performed when the event is triggered (e.g., Button Click).
18. Master Pages and Caching in ASP.NET
Master Pages
- Define a consistent layout for all pages (e.g., header, footer).
- Multiple content pages can use a single master page for layout.
- Provides separation between content and layout.
- Example:
<%@ Master Language="C#" %> <html> <head> <title><asp:ContentPlaceHolder id="title" runat="server" /></title> </head> <body> <asp:ContentPlaceHolder id="content" runat="server" /> </body> </html>
Caching
- Output Caching: Stores rendered pages or parts of pages to reduce server load.
- Data Caching: Stores data in memory for faster access.
- Object Caching: Cache individual objects.
- Example:
<%@ OutputCache Duration="60" VaryByParam="None" %>
- Caching improves performance by reducing database load and network requests.
19. Delegates vs. Events
Delegates
- Reference to a method.
- Used to pass methods as arguments.
- Can invoke multiple methods.
- Example:
public delegate void MyDelegate(string message); MyDelegate del = new MyDelegate(SendMessage); del("Hello");
- Can be multicast, calling multiple methods at once.
Events
- Built on delegates but designed for notifying multiple subscribers when something happens.
- Events can’t be invoked outside the declaring class.
- Example:
public event MyDelegate MyEvent; MyEvent?.Invoke("Hello");
- Used to implement event-driven programming (e.g., button clicks).
- Events ensure encapsulation of the event firing process.
20. Windows Programming vs. Web Programming
Windows Programming
- Runs on local machines (e.g., using Windows Forms or WPF).
- Requires installation on every machine.
- Uses local system resources (RAM, CPU, etc.).
- Typically operates in a richer UI environment.
- Example: MS Word, Excel.
Web Programming
- Runs on web browsers; does not require installation.
- Applications are hosted on a server (e.g., ASP.NET).
- Accessed by multiple users via the internet.
- Limited by browser capabilities and network speed.
- Example: Google Docs, Facebook.