Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've defined a class like this:

C#
class TextEnhancement
...
public TextEnhancement(int offset, int length)
{
   ...
}

And then I've defined a collection that uses this type as:
C#
public List<TextEnhancement> mTextEnhancements;

Normally I'd then add an element to this collection thus:
C#
mTextEnhancements.Add(new TextEnhancement(offset: 6, length: 5));

However, is it possible to also provide some helper overload that would allow me to add an element to this collection as follows?
C#
mTextEnhancements.Add(offset: 6, length 5);

Thanks.

What I have tried:

I'm totally baffled about this so I'm still confused. This is why I've turned to the C# gurus on this site to see if this can be done.
Posted
Updated 8-Jan-19 15:56pm

Create your own collection type, inheriting from Collection<t>:
C#
public class TextEnhancementCollection<TextCollection>
{
    public void Add(int offset, int length)
    {
        Add(new TextEnhancement(offset, length);
    }
}


Create your collection instead of using List<t>:
C#
TextEnhancementCollection myCollection = new TextEnhancementCollection();
myCollection.Add(6, 5);
 
Share this answer
 
Excellent!

Thanks David.

That makes total sense.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900