C# - Return type of a function is the same a the one of a parameter

Adrien Neveu

I was wondering if it's possible for a function to return a value of type specified in a parameter in C#.

Here what I want to do:

public ReturnValueUndefined function(undefinedType value)
{
    return value;
}

I want to use this because I want my function to take any class as parameter and return the same class.

Thank you!

Trevor Elliott

You want a generic function.

public T YourFunctionName<T>(T value)
{
    return value;
}

Keep in mind that the compiler can only assume that T is an object so you can only perform basic operations like Equals, ToString, etc. You can add a where constraint to the generic method to assume that the parameter is something concrete like a class or a structure.

public T YourFunctionName<T>(T value) where T : class
{
    return value;
}

Although there is a good chance that you might actually just want to use a base class or interface to treat multiple types the same. When using a generic method you will realize quickly if you can't do what you may want to do with that pattern. If your constraint brings you to the point where you must constrain your generic method to only work on a specific interface type then you probably just want the method to be part of the interface anyway.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generics - Function return type the same as the parameter type?

From Dev

What is Android Studio shortcut to show function return type, parameter type & names all at the same time?

From Dev

Can we have a generic function with return type same as input parameter type?

From Dev

std::function with void return type and templated parameter

From Java

TypeScript function return type based on input parameter

From Dev

Extension function asking for return type instead of parameter

From Dev

Accepting lambda as function parameter and extracting return type

From Dev

Return type hinting from function parameter

From Dev

Function should return type of a not-null parameter

From Dev

Accepting lambda as function parameter and extracting return type

From Java

correct return type for C function?

From Dev

Function return type conflict in C

From Dev

C++ return type of function

From Dev

How to return dynamic type depend on the parameter type in function

From Dev

composeAction in playframework: take one parameter, return the same value, why need it?

From Dev

composeAction in playframework: take one parameter, return the same value, why need it?

From Dev

How to deduce the return type of a function template base on a "function like" parameter?

From Dev

Type of parameter or return type as parameter?

From Dev

java generics return type of method should be same pass parameter passed

From Dev

return several parameter from lua C function

From Dev

block as parameter or return value for C function

From Dev

parameter passing / return of function arrays in C,

From Dev

What does return; in a C function of boolean return type return?

From Dev

C# - Send type as parameter and return a type based on it

From Dev

Meaning of a c++ function parameter type declaration

From Dev

typedef return type of template parameter's member function

From Dev

Determine the return-type of a function which is given as a templated-parameter

From Dev

Typescript: function return object type with field from parameter

From Dev

Typescript: function return object type with field from parameter

Related Related

  1. 1

    Generics - Function return type the same as the parameter type?

  2. 2

    What is Android Studio shortcut to show function return type, parameter type & names all at the same time?

  3. 3

    Can we have a generic function with return type same as input parameter type?

  4. 4

    std::function with void return type and templated parameter

  5. 5

    TypeScript function return type based on input parameter

  6. 6

    Extension function asking for return type instead of parameter

  7. 7

    Accepting lambda as function parameter and extracting return type

  8. 8

    Return type hinting from function parameter

  9. 9

    Function should return type of a not-null parameter

  10. 10

    Accepting lambda as function parameter and extracting return type

  11. 11

    correct return type for C function?

  12. 12

    Function return type conflict in C

  13. 13

    C++ return type of function

  14. 14

    How to return dynamic type depend on the parameter type in function

  15. 15

    composeAction in playframework: take one parameter, return the same value, why need it?

  16. 16

    composeAction in playframework: take one parameter, return the same value, why need it?

  17. 17

    How to deduce the return type of a function template base on a "function like" parameter?

  18. 18

    Type of parameter or return type as parameter?

  19. 19

    java generics return type of method should be same pass parameter passed

  20. 20

    return several parameter from lua C function

  21. 21

    block as parameter or return value for C function

  22. 22

    parameter passing / return of function arrays in C,

  23. 23

    What does return; in a C function of boolean return type return?

  24. 24

    C# - Send type as parameter and return a type based on it

  25. 25

    Meaning of a c++ function parameter type declaration

  26. 26

    typedef return type of template parameter's member function

  27. 27

    Determine the return-type of a function which is given as a templated-parameter

  28. 28

    Typescript: function return object type with field from parameter

  29. 29

    Typescript: function return object type with field from parameter

HotTag

Archive