C# Return type that matches type passed to function in C#

  • Thread starter Thread starter BiGyElLoWhAt
  • Start date Start date
  • Tags Tags
    Array object
Click For Summary
SUMMARY

The discussion focuses on creating a C# function that can accept an array of arbitrary types and return an array of the same type. The user is concerned about type safety when returning an Object[] and is exploring alternatives to avoid tedious individual methods for each type. The solution involves utilizing C# generics, which allows for type parameters to be specified, ensuring that the function maintains the type of the input array. The user is directed to the official Microsoft documentation on C# generics for further guidance.

PREREQUISITES
  • Understanding of C# programming language
  • Familiarity with arrays and their manipulation in C#
  • Knowledge of C# generics and their syntax
  • Basic understanding of object-oriented programming principles
NEXT STEPS
  • Learn about C# generics and how to implement them in functions
  • Explore the use of the 'params' keyword in C# for variable-length argument lists
  • Investigate the use of reflection in C# for dynamic type handling
  • Review best practices for type safety in C# collections
USEFUL FOR

C# developers, software engineers working with dynamic data structures, and anyone looking to enhance their understanding of generics in C#.

BiGyElLoWhAt
Gold Member
Messages
1,637
Reaction score
138
TL;DR
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
You might be able to use some combination of typeof() and GetType.
 
  • Like
Likes BiGyElLoWhAt

Similar threads

Replies
3
Views
1K
  • · Replies 31 ·
2
Replies
31
Views
3K
Replies
20
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 16 ·
Replies
16
Views
6K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K