How to use the `dynamic` when specifying generic type arguments in C#?

c# create instance of generic type at runtime
c# cast object to generic type at runtime
c# dynamic object example
dynamic c#
the dynamic type exposes which of the following to c#
c# dynamic type casting using reflection
c# generics
c# reflection generic type

How to use the dynamic when specifying generic type arguments in C#?

I am reading the CLR via C# book. And I come across the following paragraph:

It is also possible to use dynamic when specifying generic type arguments to a generic class (reference type), a structure (value type), an interface, a delegate, or a method. When you do this, the compiler converts dynamic to Object and applies DynamicAttribute to the various pieces of metadata where it makes sense. Note that the generic code that you are using has already been compiled and will consider the type to be Object; no dynamic dispatch will be performed because the compiler did not produce any payload code in the generic code.

As far as I understand this excerpt tells that I can use the dynamic as a type argument in (e.g.) a class definition. But after trying this out I come to a conclusion that it is no different from using any other placeholder in the type argument. So, I doubt that my understanding is correct.

using System;

namespace myprogram
{
    class A<dynamic> {
        public dynamic a;
    }

    class B {
        public Int32 b;
    }

    class C<T> {
        public T c;
    }

    class Program {
        static void Main(string[] args) {
            //Cannot implicitly convert type 'string' to 'myprogram.B' [Console.NET]csharp(CS0029)
            //A<B> a = new A<B> {a = "foo"};

            //Cannot implicitly convert type 'string' to 'myprogram.B' [Console.NET]csharp(CS0029)
            //C<B> c = new C<B> {c = "foo"};

            //as you can see it does not matter if I use the T or dynamic, the effect is the same
            //so, what is the use of using the dynamic in the class definition?
        }
    }
}

It is very important to understand the difference between type "arguments" and type "parameters".

Consider this:

class Foo<T> { } // "T" is a type parameter

...

Foo<int> f; // "int" is a type argument

Type parameters declare what types you can pass to this generic type/method. Type parameters are essentially identifiers, not an existing type. When you pass a type to a generic type/method, the type you passed is called the type argument. This is quite similar to the difference between a method parameter and argument.

So the excerpt is trying to say that given a generic type, you can pass the type dynamic to it, and it will be treated as object by the CLR. It doesn't mean that you can do this:

class A<dynamic> {

}

It means that you can do this:

var list = new List<dynamic>();

Or with the types declared in your code:

C<dynamic> c = new C<dynamic> {c = "foo"};

How to: Examine and Instantiate Generic Types with Reflection , Get an array that contains the generic type arguments, using the type definition ) or a type that has been specified for a type parameter (for� Using type dynamic (C# Programming Guide) 07/20/2015; 5 minutes to read +6; In this article. C# 4 introduces a new type, dynamic. The type is a static type, but an object of type dynamic bypasses static type checking. In most cases, it functions like it has type object.

Short answer: in your code dynamic is just a type parameter name, you're not actually passing an argument.

As far as I understand this excerpt tells that I can use the dynamic as a type argument in (e.g.) a class definition.

There are no type arguments in a class definition. In the definition of a generic type there are type parameters. When you construct a generic type these are type arguments. So this:

class A<dynamic>
{
}

var a = new A<string>();

is a generic type with one type parameter whose name is dynamic. Then follows an instantiation of the type where string is passed as a type argument to the type parameter dynamic. This:

class A<T>
{
}

var a = new A<dynamic>();

is a generic type with one type parameter whose name is T. Then follows an instantiation of the type where dynamic is passed as a type argument to the type parameter T.

You're looking for the latter, not the former.

Using type dynamic, For example, if instance method exampleMethod1 in the following code has only one parameter, the compiler recognizes that the first call to the� Introduction to C# Dynamic. To avoid checking during compile time a new type was introduced with the release of C# 4.0 called dynamic type. Basically, a type is assigned based on the expression value by the compiler whereas this dynamic type does not undergo type check during the compile time, that is it escapes the checking of type during compile time instead the type is resolved during the

You can use dynamic as an Argument, so you can use

var c = new C<dynamic>();

But you cannot use a concrete Type, to build a generic Type, like

     class A<dynamic> { }
     class B<int> { }

In underline: This you should NOT do ! You are defining an argument name here !

Actually it doesn't cause compile-error, but the word "int" as treated as a parameter name, same as there would be T. It's a good paradigm to use names starting with T for generic type parameters, not to mix it up, with any type from the Rest of your program.

As you concluded yourself, your definition of A and C are completly identical, except you are confused, cause the word dynamic has nothing to do with the type dynamic in this place.

If you want to assign a string, of course you have to create a new C<string>() or new C<object>() or any other type accepting a string.

Working with the Dynamic Type in C#, Dynamic objects let you work with structures such as JSON With the introduction of the Dynamic Language Runtime (DLR), what does this say about C#? . Dynamic object interoperability are C# types you can use to access the DLR. If you change the generic type to a string, it will pick up the second� A generic class, such as GenericList<T> listed in Introduction to Generics, cannot be used as-is because it is not really a type; it is more like a blueprint for a type. To use GenericList<T>, client code must declare and instantiate a constructed type by specifying a type argument inside the angle brackets. The type argument for this

[Solved] C# generics, the type arguments for method 'xxxxxxxx , I'm getting the following error on the caller side: The type arguments for method ' xxxxxxxx' cannot be inferred from the usage. Try specifying the� Basically, type inference for generic type parameters works based on the arguments you pass to the method.. So And() works, because you're passing in an isAdult.The compiler infers that TPred is PersonPredict as that's the type of isAdult and there are no other constraints.

Generic type parameters and dynamic types in C#, There may be times when you need to use some type in your application from a dynamically loaded assembly as a generic type parameter when you create an … for a type and that a developer that aims to use the class must specify a type LoadFrom( @"C:\Users\magnus\SomeCustomAssembly.dll" );. Use type parameters as constraints on generic classes in scenarios in which you want to enforce an inheritance relationship between two type parameters. NotNull constraint. Beginning with C# 8.0 in a nullable context, you can use the notnull constraint to specify that the type argument must be a non-nullable value type or non-nullable reference

Generics in C, C# - Value & Reference Types � C# - Anonymous Type � C# - Dynamic Types � C# - A generic class can be defined using angle brackets <>. When deriving from a generic base class, you must provide a type argument If you want the derived class to be generic then no need to specify type for the generic base class. Trying to substitute type parameter at runtime will defeat the whole purpose of type saftey , which is enforced by C# compiler.C# compiler make sures that type parameter are specified at compile time and there is no ambiguity on type arguments at runtime.I doubt you can substitue type parameter at runtime in Generic Type.Specifying type

Comments
  • A string is not A<B> or C<B> and vice-versa. This is what the compiler is telling you and it's correct.
  • Read generics like this: C<B> C is a class that has code which depends on anything that satisfies the interface of B. Now it's clear why you cannot do what you're trying because string does not satisfy the interface of B.
  • That is exactly what I was looking for when writing the question. Thank you.