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

  1. Page Request: The browser requests a page, triggering ASP.NET to create a request object.
  2. Start: ASP.NET checks if it’s a postback (return visit) or a new request.
  3. Initialization: Controls on the page are initialized, but their properties haven’t yet been restored from view state.
  4. Load: Controls are loaded with information. You can now manipulate them.
  5. Postback Event Handling: Handles events like button clicks.
  6. Rendering: ASP.NET generates the HTML output for the browser.
  7. 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)

  1. Supports one window at a time.
  2. Simpler interface (e.g., Notepad).
  3. Lower memory and processing overhead.
  4. Typically used in simpler applications.
  5. Easier to implement.

MDI (Multiple Document Interface)

  1. Supports multiple child windows.
  2. Useful for complex applications (e.g., Excel).
  3. Higher memory usage.
  4. Child windows can be minimized, maximized.
  5. 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.

  1. SELECT: Retrieves data.
  2. INSERT: Adds new data.
  3. UPDATE: Modifies existing data.
  4. 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.

  1. Forms Authentication: Like login form-based authentication.
  2. Windows Authentication: Uses the Windows user account to authenticate.
  3. Passport Authentication: Centralized authentication service.

Authorization

Determines the access level of authenticated users.

  1. Role-based Authorization: Users are assigned roles to access resources.
  2. 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

  1. Define a consistent layout for all pages (e.g., header, footer).
  2. Multiple content pages can use a single master page for layout.
  3. Provides separation between content and layout.
  4. 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

  1. Output Caching: Stores rendered pages or parts of pages to reduce server load.
  2. Data Caching: Stores data in memory for faster access.
  3. Object Caching: Cache individual objects.
  4. Example: <%@ OutputCache Duration="60" VaryByParam="None" %>
  5. Caching improves performance by reducing database load and network requests.

19. Delegates vs. Events

Delegates

  1. Reference to a method.
  2. Used to pass methods as arguments.
  3. Can invoke multiple methods.
  4. Example:
    public delegate void MyDelegate(string message);
    MyDelegate del = new MyDelegate(SendMessage);
    del("Hello");
  5. Can be multicast, calling multiple methods at once.

Events

  1. Built on delegates but designed for notifying multiple subscribers when something happens.
  2. Events can’t be invoked outside the declaring class.
  3. Example:
    public event MyDelegate MyEvent;
    MyEvent?.Invoke("Hello");
  4. Used to implement event-driven programming (e.g., button clicks).
  5. Events ensure encapsulation of the event firing process.

20. Windows Programming vs. Web Programming

Windows Programming

  1. Runs on local machines (e.g., using Windows Forms or WPF).
  2. Requires installation on every machine.
  3. Uses local system resources (RAM, CPU, etc.).
  4. Typically operates in a richer UI environment.
  5. Example: MS Word, Excel.

Web Programming

  1. Runs on web browsers; does not require installation.
  2. Applications are hosted on a server (e.g., ASP.NET).
  3. Accessed by multiple users via the internet.
  4. Limited by browser capabilities and network speed.
  5. Example: Google Docs, Facebook.