sujith karivelil
sujith karivelil

Reputation: 29036

Why can't we use dynamic methods instead for Generic(T) methods

I have Two functions defined as follows. Method 1 will return any type dynamically, method 2 will return generic types:

Method 1

public dynamic dynamicFun(int n)
        {
            switch (n)
            {
                case 0:
                    return 0;
                case 1:
                    return "N";
                case 2:
                    return 1.3;
                default:
                    return 'v';
            }
        }

Method 2:

  public static T genericFun<T>(int n)
        {
            switch (n)
            {
                case 0:
                    return (T)System.Convert.ChangeType(0, Type.GetTypeCode(typeof(T)));
                case 1:
                    return (T)System.Convert.ChangeType("N", Type.GetTypeCode(typeof(T)));
                case 2:
                    return (T)System.Convert.ChangeType(1.3, Type.GetTypeCode(typeof(T)));
                default:
                    return (T)System.Convert.ChangeType('V', Type.GetTypeCode(typeof(T)));
            }
        }

Both are doing the same functionality. The function call will be described in the image shown below: enter image description here

Now my question is that ;

Upvotes: 2

Views: 240

Answers (2)

avrFreak
avrFreak

Reputation: 9

A big plus of generics are that you can specify the type of T. E.g. void doSomething<T> () where T : abstractParentClass

Upvotes: 0

Tyler StandishMan
Tyler StandishMan

Reputation: 459

Why can't we use dynamic methods instead [of] Generic(T) methods?

It seems to me you can, proven by your examples and the runtime values show . But there's an inherent error in your thinking with the examples: you know what type is being returned in your generic method, so you're instantiating it that way. If I did var genericCase1 = genericFun<int>(1), what would be returned? An exception would probably be thrown because "N" is not an integer.

What is the difference between these two methods?

Dynamics are determined at runtime whereas generics are resolved at compile time.

Where we can use dynamic methods and where we can use generics?

I would guess anywhere you could use a method. The reason you would use one over another is really based upon the situation you're in. If you do not know the exact Type that will be returned to you at the time of writing, then go the dynamic route. You can use this when you know your dealing with a few different base/super classes that have a particular method, and you want to hit that method, but you don't care which derived class is returned from the dynamic method.

Generics could be used to avoid writing a lot of overloads for the same functionality. If you know that you want to perform the same actions on many different things, then create a generic method, and pass the types you want to do stuff with during your writing. Using Type Constraints is really great too, because now you can limit what Types are able to call this generic method, and realize any issues at compile time.

is there any advantage for generic over dynamic?

As others have said in the comments, there are time concerns, one method may take longer than another, but I suppose pieces of that would be determined by the methods implemented, and the amount of work the JIT vs compiler has to do.

Generics would also allow type safety and reduction of errors since they are resolved at compile time. Generics may also be easier to maintain as there is not as much "magic" going on.

You should probably use dynamics sparingly, because although they are powerful, used incorrectly could provide many sleepless nights debugging.


If I'm wrong on any of this, please someone correct me. As most of us out there, I am learning too, and would appreciate feedback on any issues with my current understanding.

Upvotes: 4

Related Questions