Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

May someone explain the difference between suffixing and prefixing -- and ++ in C#

Discussion in 'Scripting' started by Limesta, Jan 21, 2015.

  1. Limesta

    Limesta

    Joined:
    Mar 30, 2014
    Posts:
    4
    it seems to be very difficult to find any documentation on the difference of prefixing and suffixing -- and ++ in C#, and I was wondering if anyone could explain the difference if there is any at all.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    MSDN (here's looking at you @Tiles )

    https://msdn.microsoft.com/en-us/library/36x43w8w.aspx

    It's useful for comparisons and the sort... for example if you wanted to do a while loop that incremented something... the evaluation of the value would differ.

    Code (csharp):
    1.  
    2. int i = 0;
    3. while(i++ < 10)
    4. {
    5.     Console.WriteLine(i.ToString());
    6. }
    7.  
    8. i = 0;
    9. while(++i < 10)
    10. {
    11.     Console.WriteLine(i.ToString());
    12. }
    13.  
    The first while loop will increment 1 to 10

    The second, 1 through 9.

    Personally I avoid it, and stick to the postfix version at all times. I find it more readable... otherwise you have to remember which is which. If you have to remember, prefix evaluates before it compares (pre... before), and postfix evaluates after it compares (post... after).
     
    Last edited: Jan 21, 2015
    Limesta likes this.
  3. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    That's how I remember the distinction.
     
    Limesta likes this.
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    Yes, but the C#/.Net documentation is all compiled in a single place on MSDN and has tools to easily find entries and related entries.

    Where as... random stackoverflow members... yeah, I'll take official documentation please.
     
    Kiwasi likes this.
  6. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Eric Lippert was a C# language designer and in charge of the C# compiler until recently.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    And?

    So it's a random stackoverflow member with some credentials. Which is a piece of random trivia I'd have to know when vetting his posts... information the average person wouldn't have available to them readily without EXTRA research.

    Where as again... official documentation.
     
  8. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    You have not looked up a C# question on Stack Overflow.
     
  9. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Cripes, you sure talk a load of bollocks. You've managed to entirely dismiss tonnes of wisdom from a major contributor to the language you use. I ask you, how can you take yourself seriously after that?
     
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    Sure, I've looked up stuff on stack overflow. That's not why I'm criticizing your post.

    I'm criticizing it because you're basically saying to not use documentation because stackoverflow exists, when really the OP's issue with not finding it isn't a lack of documentation, but the result of not going to the source of documentation.
     
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    I didn't dismiss it what so ever.

    I'm saying I'm not going to search for a random stackoverflow article for something that could be quickly and readily found on official documentation.

    When the documentation fails, that's when I go to forums and the sort. Places I'd have to VET the information from. The average person doesn't know who those people are, so they don't know if it's "wisdom" or "just another crap post".

    Call me lazy, but I'll go the easiest route FIRST where a higher level of assurance that its accurate, before falling back to the more difficult route that has a much lower rate of assurance.

    If I want to know nitty gritty information about the making of the language, I'll go to said "wise" person, because the documentation doesn't really get into that. But to find out what the ++ operator does? Yeah... not going to waste my time. I wouldn't ring up Stephen Hawkings to ask what Newton's First Law is either.
     
  12. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    10,962
    ...

    How do you get anything done?

    Even the most amazingly written docs only go so far in providing you with answers to things.
     
  13. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    Your reading comprehension, it is lacking.

     
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You wouldn't ant to either. Hawkings would likely give you an accurate description of the first law, adjusted for quantum and relativity and who knows what else.

    I'm on your side on this one. I go to the documentation first. If I can't find it there I go to answers or SO. Saying you don't need the documentation because we have SO is a bit bizarre.
     
    lordofduct likes this.
  15. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    So apparently the Hawkings analogy nails it. Just read the links to MSDN and SO. While Lippert's answer may be technically more correct, it's useless for most mundane applications.

    Exactly as if you'd asked Hawkings for an explaination of Newton's first law.
     
  16. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Uh, he explains it really clearly what differences there are. Nobody's saying documentation is worthless, just people are childishly ranting that only this special toy will do and everything else isn't good enough. In reality both of course are preferable (docs + intelligent discussion).

    Yeah this is wrong but no need to respond to pokes on forums :)
     
  17. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Man people get heated fast here, dosnt matter docs or the SO post, the information is easy to find.
     
  18. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Yeah fair enough. Apologies for stoking the fire.
     
  19. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    Honestly hippocoder...

    You partook by jumping in with provoking statements like:

    Totally misrepresenting what I said. And you still misrepresent it by claiming the "only this special toy will do" statement.

    I clearly never said my special toy would only do, and actually said quite the contrary, that using both is the better option.

    So no, I'm not apologizing to you.

    I'll apologize to OP, and any other members who feel this thread got out of hand. I was extremely caffeinated yesterday, which I don't usually do (heart problems), and I may have gotten a bit ranty.

    Either way, unfollowing thread.
     
    Last edited: Jan 22, 2015
  20. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I don't require an apology nor asked for one. I'm just saying it's really dumb all round. Not just you. It's one of those special forum threads that I'm stupid for even partaking in.