Experiments: in C#: 1. Implement Jagged Array
Experiments: in C#: 1. Implement Jagged Array
class JaggedArrayExample
{
static void Main()
{
// Declare the jagged array
int[][] jaggedArray = new int[3][];
// Unboxing
int unboxedNum = (int)boxedNum;
Console.WriteLine("Unboxed value: {0}", unboxedNum);
}
}
Explanation:
The integer num is converted into an object type (boxing).
boxedNum is cast back to an integer (unboxing).
using System;
class DateManipulation
{
static void Main()
{
Console.Write("Enter number of days to add: ");
int daysToAdd = int.Parse(Console.ReadLine());
class MultipleInheritanceExample
{
static void Main()
{
Dog dog = new Dog();
dog.Speak();
dog.Walk();
}
}
Explanation:
C# does not support multiple inheritance through classes, but it
does through interfaces.
The Dog class implements both IAnimal and IWalk.
class BaseClass
{
public BaseClass()
{
Console.WriteLine("Base class constructor called.");
}
}
class InheritanceExample
{
static void Main()
{
DerivedClass obj = new DerivedClass();
}
}
Explanation:
When an object of the DerivedClass is created, the BaseClass
constructor is called first due to inheritance.
class PolymorphismExample
{
static void Main()
{
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.Speak(); // Output: Dog barks.
myCat.Speak(); // Output: Cat meows.
}
}
Explanation:
This example demonstrates runtime polymorphism using method
overriding.
The Speak method behaves differently depending on the object
instance.
public RegistrationForm()
{
this.Text = "Registration Form";
[STAThread]
static void Main()
{
Application.Run(new RegistrationForm());
}
}
Explanation:
A simple form is created with textboxes for name and email.
Clicking the "Submit" button triggers the display of user input.
[STAThread]
static void Main()
{
Application.Run(new CalculationForm());
}
}
Explanation:
The form contains buttons to compute factorial, check primality,
and generate a factorial series for the entered number.
class UserDefinedExceptionExample
{
public static void CheckAge(int age)
{
if (age < 18)
{
throw new AgeException("Age must be 18 or older.");
}
else
{
Console.WriteLine("Access granted - You are old enough!");
}
}
public RegistrationFormWithValidation()
{
this.Text = "Registration Form with Validation";
Controls.Add(nameLabel);
Controls.Add(nameBox);
Controls.Add(emailLabel);
Controls.Add(emailBox);
Controls.Add(passwordLabel);
Controls.Add(passwordBox);
Controls.Add(phoneLabel);
Controls.Add(phoneBox);
Controls.Add(submitButton);
}
if (string.IsNullOrWhiteSpace(name))
{
MessageBox.Show("Name cannot be empty.");
return;
}
if (!Regex.IsMatch(email, @"^\S+@\S+\.\S+$"))
{
MessageBox.Show("Invalid email format.");
return;
}
if (password.Length < 6)
{
MessageBox.Show("Password must be at least 6 characters
long.");
return;
}
if (!Regex.IsMatch(phone, @"^\d{10}$"))
{
MessageBox.Show("Phone number must be 10 digits.");
return;
}
MessageBox.Show("Registration Successful!");
}
[STAThread]
static void Main()
{
Application.Run(new RegistrationFormWithValidation());
}
}
[WebMethod]
public double Subtract(double a, double b)
{
return a - b;
}
[WebMethod]
public double Multiply(double a, double b)
{
return a * b;
}
[WebMethod]
public double Divide(double a, double b)
{
if (b == 0)
throw new System.DivideByZeroException("Cannot divide by
zero.");
return a / b;
}
}
Html
<asp:TextBox ID="txtNumber" runat="server" placeholder="Enter a
number"></asp:TextBox>
<asp:Button ID="btnCalculate" runat="server" Text="Find Factorial"
OnClick="btnCalculate_Click" />
<asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
Csharp
protected void btnCalculate_Click(object sender, EventArgs e)
{
int number = int.Parse(txtNumber.Text);
long factorial = 1;
for (int i = 1; i <= number; i++)
{
factorial *= i;
}
lblResult.Text = "Factorial: " + factorial;
}
Html
<asp:TextBox ID="txtAmount" runat="server" placeholder="Enter
amount"></asp:TextBox>
<asp:DropDownList ID="ddlCurrency" runat="server">
<asp:ListItem Value="USD">USD</asp:ListItem>
<asp:ListItem Value="EUR">EUR</asp:ListItem>
<asp:ListItem Value="INR">INR</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnConvert" runat="server" Text="Convert"
OnClick="btnConvert_Click" />
<asp:Label ID="lblConvertedAmount" runat="server"
Text=""></asp:Label>
Csharp
protected void btnConvert_Click(object sender, EventArgs e)
{
double amount = double.Parse(txtAmount.Text);
string currency = ddlCurrency.SelectedValue;
double convertedAmount = 0;
switch (currency)
{
case "USD":
convertedAmount = amount * 1.2; // Example conversion rate
break;
case "EUR":
convertedAmount = amount * 1.1;
break;
case "INR":
convertedAmount = amount * 90; // Hypothetical rate
break;
}
lblConvertedAmount.Text = "Converted Amount: " +
convertedAmount;
}
Html
<asp:TextBox ID="txtA" runat="server" placeholder="Enter value of
a"></asp:TextBox>
<asp:TextBox ID="txtB" runat="server" placeholder="Enter value of
b"></asp:TextBox>
<asp:TextBox ID="txtC" runat="server" placeholder="Enter value of
c"></asp:TextBox>
<asp:Button ID="btnCalculateRoots" runat="server" Text="Find Roots"
OnClick="btnCalculateRoots_Click" />
<asp:Label ID="lblRoots" runat="server" Text=""></asp:Label>
Csharp
protected void btnCalculateRoots_Click(object sender, EventArgs e)
{
double a = double.Parse(txtA.Text);
double b = double.Parse(txtB.Text);
double c = double.Parse(txtC.Text);
double discriminant = b * b - 4 * a * c;
if (discriminant < 0)
{
lblRoots.Text = "No Real Roots";
}
else
{
double root1 = (-b + Math.Sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.Sqrt(discriminant)) / (2 * a);
lblRoots.Text = $"Roots: {root1}, {root2}";
}
}
Html
<asp:TextBox ID="txtTemp" runat="server" placeholder="Enter
temperature"></asp:TextBox>
<asp:DropDownList ID="ddlConvertFrom" runat="server">
<asp:ListItem Value="Celsius">Celsius</asp:ListItem>
<asp:ListItem Value="Fahrenheit">Fahrenheit</asp:ListItem>
</asp:DropDownList>
<asp:ListBox ID="lstConvertTo" runat="server">
<asp:ListItem Value="Celsius">Celsius</asp:ListItem>
<asp:ListItem Value="Fahrenheit">Fahrenheit</asp:ListItem>
</asp:ListBox>
<asp:Button ID="btnConvertTemp" runat="server" Text="Convert"
OnClick="btnConvertTemp_Click" />
<asp:Label ID="lblTempResult" runat="server" Text=""></asp:Label>
Csharp
protected void btnConvertTemp_Click(object sender, EventArgs e)
{
double temp = double.Parse(txtTemp.Text);
string convertFrom = ddlConvertFrom.SelectedValue;
string convertTo = lstConvertTo.SelectedValue;
double convertedTemp = 0;