0% found this document useful (0 votes)
3 views7 pages

Namespace Internal Class Static Void String: Ass1 ( (Main ( Args) ( Record Choice 0

The document provides a C# program that manages student records using a Dictionary with full CRUD operations via a console interface. It also compares the type safety and performance of Dictionary with Hashtable, highlighting the advantages of generics in modern .NET applications. Additionally, it includes a demonstration of boxing and unboxing in C#.

Uploaded by

alishba124722
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Namespace Internal Class Static Void String: Ass1 ( (Main ( Args) ( Record Choice 0

The document provides a C# program that manages student records using a Dictionary with full CRUD operations via a console interface. It also compares the type safety and performance of Dictionary with Hashtable, highlighting the advantages of generics in modern .NET applications. Additionally, it includes a demonstration of boxing and unboxing in C#.

Uploaded by

alishba124722
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

QUESTION 1:

Create a C# program that uses Dictionary to manage student records (name as key, marks as value)
with full CRUD operations (Add, Retrieve, Update, Delete) through a user-friendly console interface,
then compare its type safety and performance against Hashtable to demonstrate why generics are
preferred in modern .NET applications.

PROGRAM:
namespace Ass1
{
internal class Program
{
static void Main(string[] args)
{

//using name as key and marks as value


Dictionary<string, int> record = new Dictionary<string, int>();
int choice = 0;
do
{
Console.WriteLine("""
STUDENT RECORD

1)ADD
2)RETRIEVE
3)UPDATE
4)DELETE
5)EXIT

""");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
{
add(record);
break;
}
case 2:
{
retrieve(record);
break;
}
case 3:
{
update(record);
break;
}
case 4:
{
delete(record);
break;
}
default:
Console.WriteLine("WRONG CHOICE");
break;

}
while (choice != 5);

static void add( Dictionary<string,int> record)


{

Console.WriteLine("Enter name: ");


string name = Console.ReadLine();
if (record.ContainsKey(name))
{
Console.WriteLine("This name already exists. Use UPDATE to
modify.");
return;
}
Console.WriteLine("Enter makrs: ");
int marks = int.Parse(Console.ReadLine());

record.Add(name, marks);

}
static void retrieve(Dictionary<string, int> record)
{
if (record.Count == 0)
{
Console.WriteLine("The dictionary is empty.");
return;
}
Console.WriteLine("Enter name: ");
string name = Console.ReadLine();

if (record.ContainsKey(name))
{
Console.WriteLine($"""
name:{name}
marks:{record[name]}
""");
}
else
Console.WriteLine("NOT FOUND!");
}
static void update(Dictionary<string, int> record)
{
if (record.Count == 0)
{
Console.WriteLine("The dictionary is empty.");
return;
}
Console.WriteLine("""
What do you want to update:
1)Name
2)Marks
3)Exit
""");
int choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1: // Update Name
Console.Write("Enter the name you want to update: ");
string oldKey = Console.ReadLine();

if (!record.ContainsKey(oldKey))
{
Console.WriteLine("Name not found.");
break;
}

Console.Write("Enter updated name: ");


string newKey = Console.ReadLine();

// Update key
int value = record[oldKey];
record.Remove(oldKey);
record[newKey] = value;

Console.WriteLine("Name updated successfully.");


break;

case 2: // Update Marks


Console.Write("Enter the name whose marks you want to update:
");
string name = Console.ReadLine();

if (!record.ContainsKey(name))
{
Console.WriteLine("Name not found.");
break;
}

Console.Write("Enter updated marks: ");


if (!int.TryParse(Console.ReadLine(), out int marks))
{
Console.WriteLine("Invalid input. Please enter a valid
number.");
break;
}

record[name] = marks;
Console.WriteLine("Marks updated successfully.");
break;

case 3:
return;

default:
Console.WriteLine("WRONG CHOICE");
break;

static void delete(Dictionary<string, int> record)


{
Console.Write("Enter the name you want to remove: ");
string key = Console.ReadLine();

if (record.ContainsKey(key))
{
record.Remove(key);
Console.WriteLine("Record removed successfully.");
}
else
{
Console.WriteLine("Name not found.");
}
}

}
}

COMPARISON WITH HASHTABLE


TYPE SAFETY
1. Type Safety in Dictionary<TKey, TValue>
• Dictionary<TKey, TValue> is a generic collection, meaning the key and value types are explicitly
defined when declaring the dictionary.
• This ensures that only the specified types can be used, reducing runtime errors.

Example:

Dictionary<string, int> studentMarks = new Dictionary<string, int>();


studentMarks["Alice"] = 90; // Valid
studentMarks["Bob"] = "A"; // Compilation error

Since studentMarks only accepts int values, assigning a string causes a compile-time error, making the
code safer.

2. Type Safety in Hashtable


• Hashtable stores both keys and values as object, meaning it allows different types to be stored
together.
• This requires explicit type casting when retrieving values, leading to potential runtime errors.
Example:

Hashtable studentMarks = new Hashtable();


studentMarks["Alice"] = 90;
studentMarks["Bob"] = "A"; // No compile-time error
int marks = (int)studentMarks["Bob"]; // Runtime error (InvalidCastException)

The cast to int will fail at runtime because "A" is a string, increasing the risk of errors.

PERFORMANCE
1. Performance of Dictionary<TKey, TValue>
• Dictionary<TKey, TValue> is optimized for speed using strongly-typed data and generic
collections.
• Lookups, insertions, and deletions have an average time complexity of O(1) due to
efficient hashing.
• Avoids boxing/unboxing, reducing memory overhead and improving execution speed.
Example:

Dictionary<string, int> studentMarks = new Dictionary<string, int>();


studentMarks["Alice"] = 90;
int marks = studentMarks["Alice"]; // No boxing/unboxing

2. Performance of Hashtable
• Hashtable uses object for storage, requiring boxing/unboxing when storing value types like int,
double, etc.
• This extra memory allocation and conversion slow down operations.

Boxing: The int value 90 is stored as an object, creating additional memory overhead.
Unboxing: When retrieving, the value must be cast back to int, causing performance loss.

Example:

Hashtable studentMarks = new Hashtable();


studentMarks["Alice"] = 90; // Boxing occurs
int marks = (int)studentMarks["Alice"]; // Unboxing occurs

QUESTION 2:
Demonstrate your understanding of boxing and unboxing by writing a C# program.

PROGRAM:
namespace Ass1
{
internal class Program
{
static void Main(string[] args)
{

int num = 42;


Print(num); // Boxing: int → object

object boxedValue = GetValue();


int originalValue = (int)boxedValue; // Unboxing: object → int
Console.WriteLine("Unboxed Value: " + originalValue);
}

static void Print(object obj) // Accepts any type


{
Console.WriteLine("Boxed Value: " + obj);
}
static object GetValue()
{
return 100; // Boxing: int → object
}

}
}

You might also like