Part 66 – C# Tutorial – Overloading indexers in c#

Published on September 11, 2017

Tags
c# string indexer example
c# string indexer property
c# integer indexer

The HTML and the code used in the demo, can be found at the link below.

Link for csharp, asp.net, ado.net, dotnet basics and sql server video tutorial playlists

In this video we will discuss about Overloading indexers in c#. Please watch Part 64 and Part 65 before proceeding. We will be modifying the example, that we discussed in Part 65.

Link for Part 64

Link for Part 65

In Part 65, we discussed about creating an indexer based on integer parameter.
public string this[int employeeId]
{
get
{
// Replace “]” with GREATERTHAN symbol
return listEmployees.
FirstOrDefault(x =] x.EmployeeId == employeeId).Name;
}
set
{
// Replace “]” with GREATERTHAN symbol
listEmployees.
FirstOrDefault(x =] x.EmployeeId == employeeId).Name = value;
}
}

Now let us create another indexer based on a string parameter.
public string this[string gender]
{
get
{
// Returns the total count of employees whose gender matches
// with the gender that is passed in.
// Replace “]” with GREATERTHAN symbol
return listEmployees.Count(x =] x.Gender == gender).ToString();
}
set
{
// Changes the gender of all employees whose gender matches
// with the gender that is passed in.
foreach (Employee employee in listEmployees)
{
if (employee.Gender == gender)
{
employee.Gender = value;
}
}
}
}

Please note that, indexers can be overloaded based on the number and type of parameters.

https://cafeadobro.ro/

https://www.stagebox.uk/wp-includes/depo10-bonus10/

depo 25 bonus 25

https://parfumschristianblanc.com/

https://www.barplate.com/wp-includes/js/qris/

https://hotmusic507.org/

Enjoyed this video?
"No Thanks. Please Close This Box!"