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

  • Thread starter Thread starter BiGyElLoWhAt
  • Start date Start date
  • Tags Tags
    Array object
AI Thread Summary
The discussion centers on creating a C# library for handling arrays of arbitrary types, specifically focusing on adding an empty element to the end of an array. The main challenge is to develop a function that accepts an array of any type and returns an array of the same type without losing type information. Concerns are raised about returning an Object[] type, which would result in a loss of specific type parameters. The idea of using 'var' is noted, but it cannot be returned directly. The need to potentially reinstantiate objects during iteration is mentioned, along with the complexity of dynamically accessing getters and setters. Suggestions include exploring generics as a solution to maintain type integrity while avoiding the tediousness of writing separate methods for each type. The discussion highlights the importance of leveraging C# generics to create a more efficient and type-safe implementation.
BiGyElLoWhAt
Gold Member
Messages
1,637
Reaction score
138
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
You might be able to use some combination of typeof() and GetType.
 
  • Like
Likes BiGyElLoWhAt
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top