Namespace Internal Class Static Void String: Ass1 ( (Main ( Args) ( Record Choice 0
Namespace Internal Class Static Void String: Ass1 ( (Main ( Args) ( Record Choice 0
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)
{
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);
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;
}
// Update key
int value = record[oldKey];
record.Remove(oldKey);
record[newKey] = value;
if (!record.ContainsKey(name))
{
Console.WriteLine("Name not found.");
break;
}
record[name] = marks;
Console.WriteLine("Marks updated successfully.");
break;
case 3:
return;
default:
Console.WriteLine("WRONG CHOICE");
break;
if (record.ContainsKey(key))
{
record.Remove(key);
Console.WriteLine("Record removed successfully.");
}
else
{
Console.WriteLine("Name not found.");
}
}
}
}
Example:
Since studentMarks only accepts int values, assigning a string causes a compile-time error, making the
code safer.
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:
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:
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)
{
}
}