Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Implementing Generic Methods | Generics & Reflection
Advanced C# with .NET
course content

Conteúdo do Curso

Advanced C# with .NET

Advanced C# with .NET

1. Introduction to Desktop Development with .NET
2. Interactive GUI
3. Threading
4. Generics & Reflection

book
Implementing Generic Methods

The syntax for creating a Generic Method is as follows:

cs

index

copy
123
returnType methodName<typeParameter1, typeParameter2, ..>(datatype1 arg1, ..) { // code here }

You can have one or more type parameters in a generic method.

We can execute generic methods as follows:

cs

index

copy
1
methodName<typeParameter1, ..>(arg1, ..);

Following is a summary of how we can convert a simple Swap method to a generic method, which can support any data type:

We don't necessarily need to use the type parameters (like T) for defining the arguments. In fact there can be generic methods with no arguments at all:

cs

index

copy
123
void ExampleMethod<A, B, C> () { // Code here }

It is not possible to directly use any kind of operators on arguments or variables defined using generic types:

cs

index

copy
123
void ExampleMethod<A, B> () { return A + B; // Error }
What is the correct syntax for creating a Generic method which takes in 3 type parameters?

What is the correct syntax for creating a Generic method which takes in 3 type parameters?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 2
We're sorry to hear that something went wrong. What happened?
some-alt