What is the term for empty generic parameters <,> in C#? [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# Language: generics, open/closed, bound/unbound, constructed
While doing some stuff with reflection in C#, I've noticed that some types have the type definition e.g.
Foo<,>
What is the official term for this notation?

Type names which are missing generic parameters such as List<> are referred to as unbound generic types. This question has a good summary of unbound generic types as well as some related terminology.
Depending on what context you are describing these types, some other terminology may be used. The C# specification uses the term "unbound generic type" to refer to something like T<>. The .Net framework seems to prefer the terms "generic type definition" (as dasblinkenlight pointed out, see also Type.GetGenericTypeDefinition()) or "open generic type" (see this article).

This is a type that represents a generic type definition:
var genType = typeof(Foo<int,int>);
var genTypeDef = genType.GetGenericTypeDefinition(); // Returns typeof(Foo<,>)

Related

what is <T> in System.Span<T> in c#? [duplicate]

This question already has answers here:
What is the <T> in Cast<T>() or List<T> in C#
(2 answers)
Closed 2 years ago.
I'm new into c# and I see a lot of things like System.Span<T>, System.Memory<T>, Span<T>, Memory<T>, System.Collections.Generic.IEnumerable<T> etc. in the C# documentation.
Can somebody please explain what <T> is?
Any help is appreciated.
Check Generic Parameters
In a generic type or method definition, a type parameter is a placeholder for a specific type that a client specifies when they create an instance of the generic type. 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 particular class can be any type recognized by the compiler. Any number of constructed type instances can be created, each one using a different type argument, as follows:
For instance:
GenericList<float> list1 = new GenericList<float>();
GenericList<ExampleClass> list2 = new GenericList<ExampleClass>();
GenericList<ExampleStruct> list3 = new GenericList<ExampleStruct>();
In each of these instances of GenericList, every occurrence of T in the class is substituted at run time with the type argument. By means of this substitution, we have created three separate type-safe and efficient objects using a single class definition. For more information on how this substitution is performed by the CLR
In addition to the above, you can also have constraints about the type that T can be.

What does it mean to put <> in code?

I am new to .NET C# and I was reading the code below:
var model = new TenantPageViewData<Tenant>(contentModel)
I can't understand <MyClass>, What does it mean to put <> in the code.
Also if you can guide me to documentation regarding this it will be great.
That is the syntax for specifying the type parameter for an open generic type. You can read more about generics here:
http://msdn.microsoft.com/en-us/library/512aeb7t.aspx
If you are new to C#, it will seem complicated at first, but a lot of the language makes use of this feature so it will be time very well spent learning it.
With the general documentation and answer out of the way, in this specific case, the TenantPageViewData exposes a generic parameter. You read that as "a TenantPageViewData<> of Tenant".
The signature for that class will look something like:
public class TenantPageViewData<T>
{
}
Where T is simply a placeholder you can reference in code that will, at compile-time, become strongly-typed to the type of argument you specify.
This class definition can be referred to as an "open" generic type because the placeholder T is, as yet, unrealised. The declaration var something = new TenantPageViewData<Tenant>() is a "closed" generic type because the generic argument is known to now be Tenant in this instance.
Type identity is per closed generic type, so a TenantPageViewData<Tenant> is not a TenantPageViewData<Landlord>, and static members of a generic class honour this also (which is a common pitfall of learning generics).
Other examples include List<string> usually read as "a List<> of string", and the comical Cup<T> :-)
these type of brackets I believe are used for marking generics.
http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx
In C#, the angle brackets (< and >) are most often used to enclose a parameter of a generic class. For example, the .Net framework provides a List class where 'T' denotes the type of objects that will be contained in the list. Therefore, when the list is created as
List<string> myList = new List<string>();
you are creating a usable, type-safe instance of the List class.
Some good info on it here.

Terminology on generics types [duplicate]

This question already has answers here:
What are the differences between Generics in C# and Java... and Templates in C++? [closed]
(13 answers)
Closed 8 years ago.
When I read about generic programming, often, are used this two terms:
parametrized types;
type parameters
Are there difference between them?
In Java, in the following declaration
public class Foo<T> { ... }
Foo is a parameterized type. T is a type parameter.
Using C++ terminology:
A class template corresponds to a parameterised type - it becomes a type once you specify arguments for the parameters.
A type parameter is a parameter of a template, for which the arguments are types.
Generic types are also known as parametized types.
Type parameters refers to the types associated with a generic type. For example, with
Dictionary<T1, T2>
T1 and T2 are the type parameters.

Is a generic type MyType<,> actually a type? [duplicate]

This question already has answers here:
If A<T1,T2> is a template for actual type, then why is typeof(A<,>) allowed?
(3 answers)
Closed 9 years ago.
I have some confusion with this that arose from messing around with exporting generic types in MEF
I noticed:
new Dictionary<string,bool>().GetType() == typeof(Dictionary<,>)
false
new Dictionary<string,bool>().GetType().GetGenericTypeDefinition() == typeof(Dictionary<,>)
true
Yet Dictionary<,> itself is not considered a ‘type’ as this will actually generate a compile error:
new Dictionary<string,bool> as Dictionary<,>
Type expected
new Dictionary<string,bool> is Dictionary<,>
Type expected
So my question is, is Dictionary<,> actually a type? Does .NET treat generic types differently than non-generic types?
Now in MEF I can export a generic class as
[Export(typeof(MyGenericClass<,>))]
And this would satisfy an import requirement like
[Import]
public MyGenericClass<string, long> Instance { get; set; }
I'm confused about the type-system's rules here
See What exactly is an “open generic type”. What you are referring to is called an unbound generic type and is explained in the same post. An unbound generic type is actually a type, however it can only be used within a typeof() expression. Note: Unlike C# Java allows expressions like List<?>.
Yes, MyType<,> is a type.
It's a "open generic" Type, See What exactly is an "open generic type" in .NET?

How to GetType of List<String> in C#? [duplicate]

This question already has answers here:
Closed 12 years ago.
How can I get the type of "List<String>" in C# using Type.GetType()?
I have already tried:
Type.GetType("List<String>");
Type.GetType("System.Collections.Generic.List.String"); // Or [String]
Note that I can't use typeof since the value I am getting the type of is a string.
You can't get it from "List<String>", but you can get it from Type.GetType:
Type type = Type.GetType("System.Collections.Generic.List`1[System.String]");
You're lucky in this case - both List<T> and string are in mscorlib, so we didn't have to specify the assemblies.
The `1 part specifies the arity of the type: that it has one type parameter. The bit in square brackets specifies the type arguments.
Where are you getting just List<String> from? Can you change your requirements? It's going to be easier than parsing the string, working out where the type parameters are, finding types in different namespaces and assemblies, etc. If you're working in a limited context (e.g. you only need to support a known set of types) it may be easiest to hard-code some of the details.
You can use typeof operator.
typeof(List<String>)
See this question: C# How can I get Type from a string representation
System.Type type = typeof(List<String>);
typeof(List<string>)

Categories

Resources