Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If Jon's answer isn't sufficient, here's a more general (but more involved, and probably somewhat more confusing) approach:</p> <pre><code>/// &lt;summary&gt; /// Allows a list of any type to be used to get a result of type TResult /// &lt;/summary&gt; /// &lt;typeparam name="TResult"&gt;The result type after using the list&lt;/typeparam&gt; interface IListUser&lt;TResult&gt; { TResult Use&lt;T&gt;(List&lt;T&gt; list); } /// &lt;summary&gt; /// Allows a list of any type to be used (with no return value) /// &lt;/summary&gt; interface IListUser { void Use&lt;T&gt;(List&lt;T&gt; list); } /// &lt;summary&gt; /// Here's a class that can sort lists of any type /// &lt;/summary&gt; class GenericSorter : IListUser { #region IListUser Members public void Use&lt;T&gt;(List&lt;T&gt; list) { // do generic sorting stuff here } #endregion } /// &lt;summary&gt; /// Wraps a list of some unknown type. Allows list users (either with or without return values) to use the wrapped list. /// &lt;/summary&gt; interface IExistsList { TResult Apply&lt;TResult&gt;(IListUser&lt;TResult&gt; user); void Apply(IListUser user); } /// &lt;summary&gt; /// Wraps a list of type T, hiding the type itself. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;The type of element contained in the list&lt;/typeparam&gt; class ExistsList&lt;T&gt; : IExistsList { List&lt;T&gt; list; public ExistsList(List&lt;T&gt; list) { this.list = list; } #region IExistsList Members public TResult Apply&lt;TResult&gt;(IListUser&lt;TResult&gt; user) { return user.Use(list); } public void Apply(IListUser user) { user.Use(list); } #endregion } /// &lt;summary&gt; /// Your logic goes here /// &lt;/summary&gt; class MyDataGridView { private IExistsList list; public void InitializeData&lt;T&gt;(List&lt;T&gt; list) { this.list = new ExistsList&lt;T&gt;(list); } public void Sort() { list.Apply(new GenericSorter()); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload