What is a suitable way to strongly input the return of a common function?

I am writing a filter function to return a specific type specified from a larger collection of supertypes (e.g. objects). The idea is that I give you an enumerated value and you return me all the strings, for example. you can write this without generics:

public static IEnumerable Filter(IEnumerable source, Type type)
{
    List<object> results = new List<object>();

    foreach(object o in source)
    {
        if(o != null && o.GetType() == type)
        {
            results.Add(o);
        }
    }

    return results;
}

if we want to return generics, there are several different ways to do this.

As a direct port:

public static IEnumerable<TResult> Filter<TResult>
                          (IEnumerable source, Type type)

Go to the example:

IEnumerable<TResult> Filter<TResult>
    (IEnumerable source, TResult resultType)

Ultimately, what I consider the cleanest:

public static IEnumerable<T> Filter<T>(IEnumerable source)

The second type will be called completely with parameters (and deduce the type):

Filter(myList, "exampleString");

where the final version will be called using a type specifier:

Filter<string>(myList);

What is a suitable way to forcefully return the return of a generic function where the return type is not automatically implied in the signature? (Why?)

( : , IEnumerable <T> . IEnumerable. Ts .)

+5
7

, Linq, , :

IEnumerable<T> OfType<T>(this IEnumerable enumerable);

:

List<object> objects = //...

foreach(string str in objects.OfType<string>())
{
    //...
}

, . , non.

+9

- ​​ . , , , ?

" " , , , - , , , .

+4

-

public static IEnumerable<T> Filter<T>(IEnumerable<T> source)

. IEnumerable

IEnumerable enumerable = GetMyEnumerable();
var filtered = Filter(enumerable.Cast<string>());

.

public static IEnumerable<T> Filter<T>(this IEnumerable<T> source)
...
var filtered = GetMyEnumerable().Cast<string>().Filter();

EDIT

OP , . Enumerable.OfType

var filtered = GetMyEnumerable().OfType<SomeType>();
+1

3.5, IEnumerable:

IEnumerable<string> s = someList.OfType<string>()
+1

2 . , . IEnumerable , IEnuerable.

,

public static IEnumerable<T> Filter<T>(IEnumerable<T> source)

, , T , . , ... .

public static IEnumerable<T> Filter<T>(IEnumerable<T> source) where T : class
public static IEnumerable<T> Filter<T>(IEnumerable<T> source) where T : struct
0

( , ), :

IEnumerable<T> Filter<T>(IEnumerable source)

, .

:

IEnumerable<T> Filter<T>(IEnumerable source, Type type)

, , :

:

IEnumerable<T> Filter<T>(IEnumerable source, T type)

, . , , null ( )?

, , , T. TResult, - , Func <TResult> , .

0

, , .

public static IEnumerable<T> OfType<T>(IEnumerable source) {
    foreach (object obj in source)
        if (obj is T)
            yield return (T)obj;
}

, () ,

public static IEnumerable<T> OfType<T>(IEnumerable source) {
    foreach (object obj in source) {
        T result = obj as T;
        if (result != null)
            yield return result;
    }
}
0

All Articles