CSC378 ยท 2081 ยท 10 marks
Write short notes on: a. Polymorphism b. Hidden Field [5+5]
Write short notes on: a. Polymorphism b. Hidden Field [5+5]
Quick answer
In function overloading, multiple methods share the same name but differ in the number or type of parameters.
Short Notes
a. Polymorphism (5 Marks)
Definition
The word polymorphism means "having many forms." It is the ability of a function, method, or object to behave differently in different contexts. In C#, polymorphism can be static (compile-time) or dynamic (run-time).
1. Static Polymorphism (Compile-Time Polymorphism)
The mechanism of linking a function with an object during compile time is called static binding. C# implements static polymorphism through function overloading.
Example:
using System;
namespace PolymorphismApplication {
class Printdata {
void print(int g) {
Console.WriteLine(g);
}
void print(double f) {
Console.WriteLine(f);
}
void print(string s) {
Console.WriteLine(s);
}
}
}
Here, the method print() is overloaded to handle int, double, and string data types. The compiler decides which version to call at compile time.
2. Dynamic Polymorphism (Run-Time Polymorphism)
C# allows the creation of abstract classes that provide partial class implementation. The implementation is completed when a derived class inherits from the abstract class and overrides its methods. The correct method is resolved at run time.
Key Points:
- Uses
abstractkeyword for the base class and methods. - Derived classes use
overridekeyword to provide specific implementations. - Enables method overriding.
Example:
abstract class Shape {
public abstract void Draw();
}
class Circle : Shape {
public override void Draw() {
Console.WriteLine("Drawing Circle");
}
}
class Rectangle : Shape {
public override void Draw() {
Console.WriteLine("Drawing Rectangle");
}
}
Importance of Polymorphism
- Promotes code extensibility and reusability.
- Allows a single interface to represent different underlying forms (data types or classes).
- Makes programs easier to maintain and scale.
b. Hidden Field (5 Marks)
Definition
A hidden field is an HTML form element that allows data to be stored on the client side without being displayed on the page. When the user performs an action (such as form submission), the hidden field value is passed back to the server side along with the form data.
Purpose
Sometimes we require certain data (such as a user ID or session-related value) to be available across requests without showing it to the user. Hidden fields serve this purpose by embedding the data in the HTML form invisibly.
How Hidden Fields Work in ASP.NET Core MVC
Step 1: GET Method - Set the hidden field value
The GET version of the action method creates an object and passes it to the view:
[HttpGet]
public IActionResult SetHiddenFieldValue() {
User newUser = new User() {
Id = 101,
Name = "John",
Age = 31
};
return View(newUser);
}
Step 2: View - Render the hidden field
In the view, the Id field is rendered as a hidden input so it is not visible to the user but is included in the form:
@model User
<form method="post">
<input type="hidden" name="Id" value="@Model.Id" />
<input type="text" name="Name" value="@Model.Name" />
<button type="submit">Submit</button>
</form>
Step 3: POST Method - Read the hidden field value
The POST version reads the hidden field value from IFormCollection:
[HttpPost]
public IActionResult SetHiddenFieldValue(IFormCollection keyValues) {
var id = keyValues["Id"]; // Retrieves the hidden field value
return View();
}
Key Points
| Aspect | Detail |
|---|---|
| Visibility | Not visible to the user on the page |
| Transmission | Sent to the server on form submission |
| Use Case | Storing IDs, tokens, or state data across requests |
| Security | Should not be used for sensitive data as it can be viewed in page source |
Summary
Hidden fields are a simple and effective way to persist data between HTTP requests in a stateless web environment without exposing it in the UI. In ASP.NET Core MVC, they are commonly used with IFormCollection or model binding to retrieve the stored values on the server side.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.