0

In order to work around an unusual situation, I need a function that does this:

object Identity(object x) { return x; }

It would solve my problem with a third-party library that I cannot change. I really don't want to create a new .NET DLL file just to introduce this function. Is there anything like this in any of the standard .NET libraries?

I am not writing any .NET code. I am calling into libraries from another wrapper environment. All I can do is send commands to instantiate objects or invoke methods/properties of objects. There's no place I can just write this function for myself. This identity function would help me avoid the garbage collector of the calling environment thinking that the object can be released.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
srm
  • 3,062
  • 16
  • 30
  • 7
    You need a function that returns the one argument you pass to it...? – Paul Suart Oct 13 '21 at 20:40
  • 8
    Can you describe the problem you are facing? The solution you are asking for seems trivial. The language and the libraries don't have _standalone_ functions like you describe. It's easy to write (as you found out in your question). What's wrong with the one you wrote? Another way to do it would be to say something like `Func Identity = x => x`. – Flydog57 Oct 13 '21 at 20:42
  • 9
    How will it solve your problem, and what is that problem? Why can you not write the method yourself, or use an anonymous function, a lambda `x => x`? – Jeppe Stig Nielsen Oct 13 '21 at 20:43
  • @srm you need to provide more detail as in your sample - just don't do it as you are returning the same object you pass in so why call it at all....you see the problem now with the question? Provide more details... – Sorceri Oct 13 '21 at 20:55
  • @Flydog57 I'm calling into the .NET DLL from outside of .NET... I can't just write the function because currently, I don't have any .NET DLLs that I own the source code for. – srm Oct 13 '21 at 21:04
  • 1
    @Sorceri Yes, that is exactly what I need. A no op function that just returns its argument. I'm working in an environment that wraps .NET, and I'm trying to compensate for a particular behavior by supplying that no-op. – srm Oct 13 '21 at 21:05
  • 3
    I don't think this would prevent the GC from releasing your object. You just need a persistent object to point to it – Nigel Oct 13 '21 at 21:11
  • 1
    @NigelBess The GC referred to in this case isn't the .NET GC, it's one of the calling environment. – Joe Sewell Oct 13 '21 at 21:15
  • 2
    Does this answer your question? [LINQ identity function?](https://stackoverflow.com/questions/1466689/linq-identity-function) – Dmitry Oct 13 '21 at 21:29
  • 7
    This question is being discussed on [meta](https://meta.stackoverflow.com/questions/412250). – cigien Oct 13 '21 at 21:58
  • 3
    Are you looking for something like `(new object[]{ x }).First()`? Although I have to add that you would probably get better results if you ask for help with your initial problem instead of asking about a potential workaround. The whole question looks very much like an XY problem. – BDL Oct 13 '21 at 22:07
  • *All I can do is send commands to instantiate objects or invoke methods/properties of objects* - then you can be like `Type object_type = Type.GetType("System.Object");`, `ParameterExpression param = Expression.Parameter(object_type);`, `LambdaExpression return_statement = Expression.Lambda(param, param);`, `Func func = (Func)return_statement.Compile();`? Okay, the latter involves the generics syntax; but depending on your call system you can get away with passing the result of just `return_statement.Compile()` to your func-expecting method? – GSerg Oct 13 '21 at 22:35
  • @AlexeiLevenkov Doesn't seem like a good duplicate target, except for maybe [this answer](https://stackoverflow.com/a/32141902/11683), but then again, the comment below is valid. – GSerg Oct 13 '21 at 22:41
  • 1
    @GSerg the questions are essentially identical - so I don't see how duplicate does not work. Clearly any answer to this question must be present in the one I picked as duplicate - so if for some reason there is existing documented method OP would get answer from the duplicate (like F# one). Asking for private/internal methods would likely qualify as a new question... but "what undocumented feature I can use" questions rarely succeed. There is also variant "how to construct identity using just ..." that OP is started in the question - that would be ok (slightly code-golf so) question. – Alexei Levenkov Oct 14 '21 at 03:59

1 Answers1

4

According to this answer there's an identity function in the F# FSharp.Core library, which isn't specifically part of the base class library, but if you're in a position to import existing code and still don't want to write your own DLL for this situation, it might be useful. From the F# source code:

[<CompiledName("Identity")>]
let id x = x
Joe Sewell
  • 6,067
  • 1
  • 21
  • 34