0% found this document useful (0 votes)
3 views1 page

Program 17

The document presents a C# program that defines a class 'StudentRecords' for managing student names and grades using two indexers: one for integer indices and another for string keys. It includes methods to add students and retrieve their grades. The main program demonstrates adding students and accessing their information through both indexers.

Uploaded by

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

Program 17

The document presents a C# program that defines a class 'StudentRecords' for managing student names and grades using two indexers: one for integer indices and another for string keys. It includes methods to add students and retrieve their grades. The main program demonstrates adding students and accessing their information through both indexers.

Uploaded by

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

using System;

using System.Collections.Generic;

class StudentRecords
{
private Dictionary<string, string> studentData = new Dictionary<string,
string>();
private List<string> studentNames = new List<string>();

// Indexer with integer index


public string this[int index]
{
get { return studentNames[index]; }
set { studentNames[index] = value; }
}

// Indexer with string key


public string this[string name]
{
get { return studentData.ContainsKey(name) ? studentData[name] : "Not
Found"; }
set { studentData[name] = value; }
}

public void AddStudent(string name, string grade)


{
studentNames.Add(name);
studentData[name] = grade;
}
}

class Program
{
static void Main()
{
StudentRecords records = new StudentRecords();
records.AddStudent("John", "A");
records.AddStudent("Alice", "B");

Console.WriteLine("Student at index 0: " + records[0]); // Access using int


index
Console.WriteLine("Alice's Grade: " + records["Alice"]); // Access using
string key

records["Alice"] = "A+";
Console.WriteLine("Alice's updated Grade: " + records["Alice"]);
}
}

You might also like