Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Prefix vs. Postfix Increments

Prefix vs. Postfix Increments

Scheduled Pinned Locked Moved C#
visual-studioquestion
4 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IndifferentDisdain
    wrote on last edited by
    #1

    I've noticed I've been using a lot of prefix incrementors in code lately (esp. for counters), simple e.g.:

    int counter = 0;
    foreach (var thing in things) {
    thingThatCares.TellMeWhereIAm(++counter);
    }

    I typically don't see a lot of prefix operands in other folks' code, so I'm wondering if there's a better way of doing things like that, or it's just stylistic, etc.?

    L P C 3 Replies Last reply
    0
    • I IndifferentDisdain

      I've noticed I've been using a lot of prefix incrementors in code lately (esp. for counters), simple e.g.:

      int counter = 0;
      foreach (var thing in things) {
      thingThatCares.TellMeWhereIAm(++counter);
      }

      I typically don't see a lot of prefix operands in other folks' code, so I'm wondering if there's a better way of doing things like that, or it's just stylistic, etc.?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It is better and faster then postfix when you do not need the original value. Although, most modern compilers would optimise a postfix expression for the same reason.

      Use the best guess

      1 Reply Last reply
      0
      • I IndifferentDisdain

        I've noticed I've been using a lot of prefix incrementors in code lately (esp. for counters), simple e.g.:

        int counter = 0;
        foreach (var thing in things) {
        thingThatCares.TellMeWhereIAm(++counter);
        }

        I typically don't see a lot of prefix operands in other folks' code, so I'm wondering if there's a better way of doing things like that, or it's just stylistic, etc.?

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        A very Basic doubt in C operators[^]

        1 Reply Last reply
        0
        • I IndifferentDisdain

          I've noticed I've been using a lot of prefix incrementors in code lately (esp. for counters), simple e.g.:

          int counter = 0;
          foreach (var thing in things) {
          thingThatCares.TellMeWhereIAm(++counter);
          }

          I typically don't see a lot of prefix operands in other folks' code, so I'm wondering if there's a better way of doing things like that, or it's just stylistic, etc.?

          C Offline
          C Offline
          Clifford Nelson
          wrote on last edited by
          #4

          There should be no difference except that one happens after the operation, the other before. However, in a for loop it does not matter:

          for (int i = 0; i < 3; ++i)
          Console.Write(i);

          for (int i = 0; i < 3; i++)
          Console.Write(i);

          Yields the same result. However these do not:

          for (int i = 0; i < 3;)
          Console.WriteLine(i++);

          for (int i = 0; i < 3;)
          Console.WriteLine(++i);

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups