[Solved] when to use the “” in C# [closed]


<> Is used in C# generics to declare generic types.

Ex. for a list.

List<int> would create a list of ints.

To explain it further you could have a type like this:

public class MyGenericType<T>
{
    public T MyGenericProperty { get; set; }
}

In which case you could do something like this:

var myGenericIntType = new MyGenericType<int>();
myGenericIntType.MyGenericProperty = 10;

var myGenericStringType = new MyGenericType<string>();
myGenericIntType.MyGenericProperty = "Hello World!";

To be even more specific, you can actually use <>, but it’s mostly used to check if generic types are the same, generally with nullable types to check if nullable types are of a specific type.

Ex. if you want to check if MyGenericType<string> is actually MyGenericType<T> then you can do something like this:

if (myGenericStringType.GetType() == typeof(MyGenericType<>))
{
    // myGenericStringType is of type MyGenericType.
}

0

solved when to use the “<>” in C# [closed]