I think this question is very nuanced, and doesn't have a single, generic answer. Age is part of this; the answer for a four year old would be very different than for a twelve year old. Another major factor is the what; sharing ice cream is not the same thing as helping with housework or homework.
As a simple, one-to-one equivalency, I do not recommend "an eye for an eye" parenting. It's not going to teach what you are trying to teach - it is mostly going to just be irritating. At twelve, this is somewhat less true; a much younger child would simply not get the lesson at all, but a twelve year old will at least understand the connection. That said, unless it is part of a more broad approach, it won't be particularly effective.
As part of a overall strategy to teach cooperative behavior, though, it can be effective to use particular examples to help a child understand how it feels to be on the receiving end of "selfish" behavior. By "overall strategy", I mean not just denying the child a taste of your ice cream, but when it comes up on both sides talking about the emotions involved and the empathy the child should be capable of showing. It also needs to be clearly and directly related to the behavior - and in a way that you can, and do, relate back for the child.
We do something similar to this with our older child (who is a few years younger, but in the same ballpark), who definitely isn't on the same level sharing-wise as his (slightly) younger brother. A very common thing to do would be for him to eat all of his ice cream, and then see his slower brother have some left, and ask for a taste; inevitably his brother says "yes", but then of course doesn't get reciprocation.
We don't get involved in that interaction directly, though we do point out to him that he should've considered offering a taste to his brother prior. But what we might do, when he asks us for a taste without offering reciprocation, is to initially say no ("I don't see you offering me a taste!", or sometimes, "You didn't offer me a taste!"), and then when he inevitably realizes the lesson, relent and let him, with a gentle reminder to think of others.
Larger issues, though, I would never connect that way. If he needs help on homework he gets it, no matter what, that's our job. Housework might or might not qualify; if he refused to help his brother with something, and he then asks for help-as-in-make-the-job-shorter, generally we'll tell him that if he'd wanted help on this, he should've helped before. Help-as-in-doing something that he can't do effectively, or help understanding how to do something correctly, is always available.
One thing that we do try to be careful about, though, is not putting him in a position where it is impossible to fix something significant. The point after all is to learn the lesson and change future behavior, not to "punish"; so if he's gotten himself into a hole that he can't easily get out of, it's not a good idea to stick to principles so to speak. Provide the help with an emphasis on what happens next time.
Ultimately, the point is to combine teaching empathy by talking about emotions and behavior, with some assistance understanding what the other person is actually feeling. It's not to retaliate or punish. The child will learn most effectively by understanding the feelings.