Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net - passing generic lists

I have a utility class that takes a generic list as a parameter.

Code looks like:

Function DoStuff(collection as Object, elt as Object)
   ...
   collection.Add(elt)
   ...
End Function

This is called with:

DoStuff( List(Of Foo), new Foo() )
DoStuff( List(Of Bar), new Bar() )

There are about a dozen different types.

Currently, passing as Object results in a Late bound resolution warning, although it runs fine.

I've tried different ways to pass in collection and elt (Foo and Bar both extend a base class) but can't seem to figure out the "proper" way to do it.

Ideas?

like image 201
chris Avatar asked Apr 16 '26 07:04

chris


1 Answers

I think you're looking for something like this.

Public Sub DoStuff(Of T)(collection As List(Of T), elt As T)
    ...
    collection.Add(elt)
    ...
End Function
like image 141
womp Avatar answered Apr 18 '26 21:04

womp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!