Return type that matches type passed to function in C#

In summary, the conversation involves a programmer working on a project in C# to create a personal library of functions that can act on arrays of various types. The main challenge is to add an empty element to the end of an array of arbitrary type and return the same type. The solution discussed is to use generics, specifically the typeof() and GetType() methods. This allows for a more efficient and dynamic approach to handling different types of objects in the array.
  • #1
BiGyElLoWhAt
Gold Member
1,622
131
TL;DR Summary
I can't return var, but I don't know what type is passed to my function. I want to return the same type passed to it.
I am working on a project in C# that will be using arrays of various length that need to be determined at runtime.

I am attempting to write a small personal library of functions that act on arrays of various types.

Right now, I am trying to add an empty element to the end of an array, of arbitrary type (mostly different kinds of objects).

How can I make my function take in an array of arbitrary type, and return the same type. I'm pretty sure if I return Object[] that my return variable will be typeless, and I'm not sure how that will handle all of the objects already in the array, i.e. their parameters.

My original thought was to simply use var, but I can't return that. Do I need to reinstantiate each object as I loop through? If so, how do I dynamically call all the getters and setters?

I could potentially write individual methods for all different types, but that seems very tedious. I feel like there should be a better way.
 
Technology news on Phys.org
  • #2
You might be able to use some combination of typeof() and GetType.
 
  • Like
Likes BiGyElLoWhAt
  • #3
  • Like
Likes BiGyElLoWhAt and Mark44

1. What is a return type in C#?

A return type is the data type of the value that a function will return when it is called. In C#, all functions must have a return type, even if it is void, which indicates that the function does not return a value.

2. Can the return type of a function in C# be different from the type passed to it?

Yes, the return type of a function in C# can be different from the type passed to it. For example, a function can take in an integer as a parameter, but return a string as its result.

3. How do you declare the return type of a function in C#?

To declare the return type of a function in C#, you use the return type keyword before the function name. For example, if you want to return an integer, you would use the keyword "int" before the function name.

4. Can the return type of a function in C# be a user-defined type?

Yes, the return type of a function in C# can be a user-defined type. This means that you can create your own custom data type and use it as the return type for a function.

5. What happens if the return type of a function in C# does not match the type passed to it?

If the return type of a function in C# does not match the type passed to it, the code will not compile. The compiler will throw an error indicating that the return type does not match the expected type.

Similar threads

Replies
3
Views
768
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
16
Views
4K
  • Programming and Computer Science
Replies
30
Views
4K
Back
Top