I have a class Age with properties “AgeID” and “Name”, and this code:
public static MvcHtmlString AgeToCommaSeparated(this HtmlHelper htmlHelper, IEnumerable<Age> AgeList)
{
var sb = new StringBuilder();
foreach (var age in AgeList)
{
sb.Append(age.Name);
sb.Append(",");
}
return new MvcHtmlString(sb.ToString().TrimEnd(','));
}
Then I have lots of other classes similiar to Age, like Gender with “GenderID” and “Name”.
Is there a way to make the AgeToCommaSeparated function generic so I can pass any class to it?
Why don’t you create a common interface, something like this: