Generic type where T can be anything

123498

Is it possible to write one generic c# method which will be look like this:

MyClass.Function1<string>()
MyClass.Function1<IEnumerable<string>>()
MyClass.Function1<IDictionary<string, string>>()
MyClass.Function1<IDictionary<string, IEnumerable<string>>>()

T can be anything

T = string
T = IEnumerable<string>
T = IDictionary<string, string>
T = IDictionary<string, IEnumerable<string>>

Second question

Can I create one static method will be not depends on type?

string GetSth().Function1();
IEnumerable<string> GetSth().Function1()
IDictionary<string, string> GetSth().Function1()
IDictionary<string, IEnumerable<string>> GetSth().Function1()
Shadmehr

For First question define a generic class with where to shared interface such as IEnumerable or without any where clause :

public class MyClass
{
    public static string Function1<T>()
    {
        return typeof(T).FullName;
    }
    public static string Function2<T>() where T : IEnumerable
    {
        return typeof(T).FullName;
    }
}

And For second one define an Extension Methods :

public static class MyClass_Extensions
{
    public static string StaticMethod1(this object obj)
    {
        return obj?.ToString();
    }

    public static string StaticMethod2(this IEnumerable obj)
    {
        return obj?.ToString();
    }
}

So we can write this test method:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(MyClass.Function1<string>());
        Console.WriteLine(MyClass.Function1<IEnumerable<string>>());
        Console.WriteLine(MyClass.Function1<IDictionary<string, string>>());
        Console.WriteLine(MyClass.Function1<IDictionary<string, IEnumerable<string>>>());

        Console.WriteLine(MyClass.Function2<string>());
        Console.WriteLine(MyClass.Function2<IEnumerable<string>>());
        Console.WriteLine(MyClass.Function2<IDictionary<string, string>>());
        Console.WriteLine(MyClass.Function2<IDictionary<string, IEnumerable<string>>>());

        // Static Method:
        var strObj = "string";
        Console.WriteLine(strObj.StaticMethod1());
        Console.WriteLine(strObj.StaticMethod2());

        IEnumerable<string> listObj = new List<string>();
        Console.WriteLine(listObj.StaticMethod1());
        Console.WriteLine(listObj.StaticMethod2());

        IDictionary<string, string> dicObj = new Dictionary<string, string>();
        Console.WriteLine(dicObj.StaticMethod1());
        Console.WriteLine(dicObj.StaticMethod2());

        IDictionary<string, IEnumerable<string>> dicLisObj = new Dictionary<string, IEnumerable<string>>();
        Console.WriteLine(dicLisObj.StaticMethod1());
        Console.WriteLine(dicLisObj.StaticMethod2());
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't a type parameter be anything?

From Dev

Can't serialize generic type

From Dev

(In Scala,) Is there anything that can be done with generic type parameters of classes but not with abstract type members?

From Dev

Why can't we extend a generic type?

From Dev

Compilation error: Type List can't be Generic

From Dev

Why can't I cast `this` to a generic type?

From Dev

Why can't we extend a generic type?

From Dev

Can't infer generic type "? super SomeClass"

From Dev

Compilation error: Type List can't be Generic

From Dev

sublime press key "escape" can't type anything

From Dev

How to create an Action<T> with Reflection where T is a discovered generic Type

From Dev

Where to put generic type

From Dev

Can you restrict a generic to T where T is a trait implemented by Self?

From Dev

Why can't a protocol not be used as a type for a generic type in swift?

From Dev

Can't use function type as generic type constraint in Swift

From Java

TypeScript can't infer generic type when T is conditional

From Dev

Why can't I return the generic type T from this function?

From Dev

In Rails, how can you use a .where('rooms.id not in (?), ____), when ____ doesn't return anything?

From Dev

Why can't I create an array of an inner class of a generic type?

From Dev

Why can't I use covariance with two generic type parameters?

From Dev

Why can't I use a Guid as a generic type constraint?

From Dev

Why can't I cast one instantiation of a generic type to another?

From Dev

Can't invoke protocol method on generic type restricted by other protocol

From Dev

For which reason compiler can't infer generic class constructor type?

From Dev

Why can't I convert this type into a generic one?

From Dev

C# generics: what's the point of the "X<T> where T: X<T>" generic type constraint?

From Dev

constraint generic type T and K where K extends keyof T and T[K] is boolean?

From Dev

Get the type of generic T

From Dev

After installing Ubuntu 12.04.3, I get a message "Welcome To Grub!" but I can't type or do anything

Related Related

  1. 1

    Why can't a type parameter be anything?

  2. 2

    Can't serialize generic type

  3. 3

    (In Scala,) Is there anything that can be done with generic type parameters of classes but not with abstract type members?

  4. 4

    Why can't we extend a generic type?

  5. 5

    Compilation error: Type List can't be Generic

  6. 6

    Why can't I cast `this` to a generic type?

  7. 7

    Why can't we extend a generic type?

  8. 8

    Can't infer generic type "? super SomeClass"

  9. 9

    Compilation error: Type List can't be Generic

  10. 10

    sublime press key "escape" can't type anything

  11. 11

    How to create an Action<T> with Reflection where T is a discovered generic Type

  12. 12

    Where to put generic type

  13. 13

    Can you restrict a generic to T where T is a trait implemented by Self?

  14. 14

    Why can't a protocol not be used as a type for a generic type in swift?

  15. 15

    Can't use function type as generic type constraint in Swift

  16. 16

    TypeScript can't infer generic type when T is conditional

  17. 17

    Why can't I return the generic type T from this function?

  18. 18

    In Rails, how can you use a .where('rooms.id not in (?), ____), when ____ doesn't return anything?

  19. 19

    Why can't I create an array of an inner class of a generic type?

  20. 20

    Why can't I use covariance with two generic type parameters?

  21. 21

    Why can't I use a Guid as a generic type constraint?

  22. 22

    Why can't I cast one instantiation of a generic type to another?

  23. 23

    Can't invoke protocol method on generic type restricted by other protocol

  24. 24

    For which reason compiler can't infer generic class constructor type?

  25. 25

    Why can't I convert this type into a generic one?

  26. 26

    C# generics: what's the point of the "X<T> where T: X<T>" generic type constraint?

  27. 27

    constraint generic type T and K where K extends keyof T and T[K] is boolean?

  28. 28

    Get the type of generic T

  29. 29

    After installing Ubuntu 12.04.3, I get a message "Welcome To Grub!" but I can't type or do anything

HotTag

Archive