13

This question was closed as needing more information: Does C# have an identity function somewhere?

I just need to know if such a function exists. I don't know why the details of why I need it are relevant -- adding such details actually seemed to me to be against the spirit of Stack Overflow (ask questions, get answers), so I left them out at first. Now I've added them. What more information is required for the function? The F# answer is useful and I'll use it if I don't find anything else, but if someone finds one in the standard libraries, I'd appreciate knowing about it.

17
  • 8
    For the record, the initial question did indeed seem odd because it basically answered itself (by already defining the desired identity function). Such issues are often a hint that the asker doesn't quite know what they are looking for, or may have trouble understanding answers. The edit seems to have cleared that up now, though. Commented Oct 13, 2021 at 22:13
  • 19
    @MisterMiyagi an identity function is a known entity. Yes, it's extremely easy to define "by hand" but it's also a crucial building block when it comes to using functional programming. It's not at all odd to ask if it already exists. There are many programming utilities that are very easy to custom build yet already exist as part of standard libraries for convenience.
    – VLAZ
    Commented Oct 13, 2021 at 22:23
  • 8
    @VLAZ Pardon? It's rather odd to be told what is not at all odd to me. Commented Oct 13, 2021 at 22:48
  • @VLAZ So is there any reason not to ask for a reopen in your opinion?
    – srm
    Commented Oct 13, 2021 at 22:54
  • 5
    For the record, the question has since been reopened and newly closed as a duplicate. Commented Oct 13, 2021 at 22:56
  • 3
    @MisterMiyagi You said the initial question seemed odd. I'm saying it doesn't. It's not different to asking if any other common tool is found in a language. For example, here is a question about Pair in Java. It can be implemented in about 5 lines of code. It's a common tool used by programmers, similar to an identity function.
    – VLAZ
    Commented Oct 14, 2021 at 5:42
  • 5
    @VLAZ You are now for the second time asserting that you know better than me what my personal impression was, and practically that it is impossible to have that impression. Please stop that – it will not lead to a productive discussion. The question did allude to a practical issue the OP wanted to solve, not an inquiry of curiosity or design improvement, and in that regard I feel asking for something they already have is… odd. Commented Oct 14, 2021 at 6:45
  • @VLAZ More like 30 lines of code. (Or one really long line.)
    – khelwood
    Commented Oct 14, 2021 at 6:51
  • 5
    @MisterMiyagi I'm trying to realign your impression. Because I don't think "Does X exist in <language>" should be viewed as odd. Something other users evidently also thought and voted to close the question based such an impression. I am trying to say that this shouldn't happen. The Pair question is also in the exact same vein but it doesn't contain code. Are you saying that if it did it wouldn't be a practical issue?
    – VLAZ
    Commented Oct 14, 2021 at 7:06
  • 1
    @khelwood 8 lines two of which are closing brackets. Fairly minimal and you can definitely argue for more things like getters/setters, hashCode, equals, etc. However, it is a fully functional Pair. The rest doesn't make it "more" of a pair - mostly boilerplate to make it "more Java"-y. Using Lombok annotations a "fully featured" Pair is about as long. Still, I'd rather just consume this class than write it. But the point is - ability to implement something shouldn't make searching for a ready made one off-topic.
    – VLAZ
    Commented Oct 14, 2021 at 7:33
  • 3
    @VLAZ First off an entire Pair type seems a lot more complex to me than what is x => x in many languages. More importantly though, the identity question is framed that "it would solve my problem with a third-party library": there is a practical problem they want to solve, and the Identity is just an incidental piece – such questions are often XY. The other odd, very practical thing is that they "don't want to create a new .NET DLL"; creating program parts is usually the entire point of programming. Commented Oct 14, 2021 at 7:40
  • @VLAZ Not what I'd call "fully functional" if it doesn't even implement equality. When people ask for a Pair class they want something that works properly as a data type; not something that can hold two objects and that's about it.
    – khelwood
    Commented Oct 14, 2021 at 7:48
  • 3
    So, if I answer "yes" to that question, you will go through all the 35000 classes and 250000 methods in C# and find it yourself? If so, you could do that without knowing the answer to the question. Commented Oct 15, 2021 at 8:58
  • 4
    Not joking at all. I think you just need to ask better questions and SO is a great place to get feedback on the way you ask questions. The original question over there is even worse: "Does anyone know of anything like this?". That's a useless question, because the answer does not help you nor anyone else: Yes, someone knows. Now what? "Does C# have an identity function somewhere?": see before. The question you should have asked is: "Which method in which class of which assembly of the .NET BCL or FCL provides identity?" If you get an answer, you and everyone else has everything needed. Commented Oct 15, 2021 at 13:38
  • 1
    @galdin It's not about what the answerer can provide, it's about what the asker requests. Sure most people will provide the background anyway, but if that's what the question is actually about – it should be asking for it. Commented Oct 15, 2021 at 14:08

2 Answers 2

5

The question is basically just a search question and can be answered with a simple yes/no (yes is easier and should ideally be accompanied by a link to documentation, no is a tiny bit trickier and would need to be accompanied by some search description to show that no is indeed the right answer), one should really search before asking such questions. Otherwise, that simply translates to "do my work for me".

Apart from that, the question is ontopic in the sense that questions for the existence of language features are ontopic. People might have seen it as not very useful and typically "How to achieve something" questions are resulting in much more useful knowledge than "Does feature X exist" questions but that is not a reason to close. The closing of this question as unclear must have been a mistake. In the meantime, it was reopened.

People might also have seen it as a typical XY problem, i.e. your desire for this specific feature may hint that you assume this to be the solution to another problem you have, which may be solved better differently. Context (which has been added) is very useful for this.

The one thing this question is missing though is showing research, which might result in downvotes.

-1

Basically "no" is not an acceptable answer by itself - there should be some reasonable alternative suggested or good explanation of why something is not possible.

In your case, the answer to the question in the title is essentially "no" (which is covered in the duplicate I picked as suggested in the answer and comments), as there is no documented method that does what you want. The potential (likely purely opinion based) explanation of why there is no such method should also be added to the existing "Identity" question.

Your question at this point is half-way to becoming a different one "how to implement Identity using just ..." - that is a reasonable way to clarify the question in the first place and stop it from being a duplicate of the one I picked. I would recommend clarifying what exactly you can and can't use - there are couple of ways already suggested in the comments similar to Enumerable.Repeat(x,1).First() which should help to narrow down what you consider allowed operations. Additionally, as for any duplicate closure it's helpful to add something like

I looked at LINQ identity function and it sounds like there is no existing function except on from F# which is not really useful for me as {some reasoning like - it is not part of default install} and I can just add my own assembly with the method I need which I'm trying to avoid.

Please note that the question may look like a code-golf request if you follow that route. If whatever strange environment you use is more or less legal and public it may be better to disclose it as the question may look more practical (use your discretion here).

5
  • 19
    This is not correct. "No" is a perfectly acceptable answer to a question on Stack Overflow, if the question is "does X exist" or "can I do X", etc. Elaboration is obviously good, but sometimes not possible, or at least not possible to a significant extent. You may downvote such an answer if you think it is not particularly useful, but it is a valid one.
    – TylerH
    Commented Oct 14, 2021 at 13:58
  • 2
    While a "No" may fit the criteria of "endeavoring to answer the question", it is the least generous contribution that a potentially correct answer can be. We certainly don't want this type of behavior to be upvoted / role modeled / copied through out the network. Being correct may have been enough back in 2009, but we are in a new era now where education is the goal. Is a pure "No" a "green tick - acceptable answer" technically yes. Is a pure "No" answer worthy of a green tick? I would never give an upvote or green tick to such a selfish answer -- even if it was correct. Commented Oct 14, 2021 at 22:04
  • 3
    An answer is truly helpful when it educates. Expanding on the "why" and "when" and explaining known edge cases is what will make the page truly valuable on SO because it can be used to close future duplicates that fall into the scope of the explanation. Remember, everyone, we are not here to help 1 person; we are here to help thousands of people. Commented Oct 14, 2021 at 22:06
  • 2
    @mickmackusa I strongly disagree and if "education is the key", then it's not the SO I signed up for and supported for years. The whole SO philosophy is that INSTEAD of general education (there's plenty of that in the Internet, and are all failures in comparison with SO) we embrace asking and answering very specific questions. Some people will just copy/paste to keep a legacy system working. Some will want to learn more. Some already know much more, but just forgot the exact function name and SO is simpler than man. The value of SO is in the format: specific question / targeted answer.
    – fdreger
    Commented Oct 15, 2021 at 9:49
  • 1
    No worries @fdreger, I come to MSO when I want to read opinions that repulse me. I was somewhat relieved to see that you are not a frequent poster and in your most recent answers, you did not write minimal/selfish posts. Commented Oct 15, 2021 at 11:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .