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. Existance pointers in c#

Existance pointers in c#

Scheduled Pinned Locked Moved C#
csharpc++data-structuresquestion
6 Posts 6 Posters 0 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.
  • M Offline
    M Offline
    Member 7744871
    wrote on last edited by
    #1

    hi, does in c# exist a type of variable that is like pointers in c++? I mean, in c++, if I use a pointer to a byte, I can add 1 to this pointer and, in this way, move it along all the array, in c#, is there a way to do a similar thing? Thank you.

    OriginalGriffO F L Richard DeemingR A 5 Replies Last reply
    0
    • M Member 7744871

      hi, does in c# exist a type of variable that is like pointers in c++? I mean, in c++, if I use a pointer to a byte, I can add 1 to this pointer and, in this way, move it along all the array, in c#, is there a way to do a similar thing? Thank you.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Yes. They are called "pointers": Pointer types - C# Programming Guide | Microsoft Docs[^] Do note that you need to specify unsafe for yoru code in order to use them, and you need to take precautions that the memory won't be moved by the GC while you are using the pointers. If you don't need 'em, don't use 'em: and that's "real need" not "I need this to be like my C++ code".

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • M Member 7744871

        hi, does in c# exist a type of variable that is like pointers in c++? I mean, in c++, if I use a pointer to a byte, I can add 1 to this pointer and, in this way, move it along all the array, in c#, is there a way to do a similar thing? Thank you.

        F Offline
        F Offline
        F ES Sitecore
        wrote on last edited by
        #3

        There's no need to do things like that in c#, so if you find yourself doing things like that then you probably need to rethink your solution and find one more suitable to the language.

        1 Reply Last reply
        0
        • M Member 7744871

          hi, does in c# exist a type of variable that is like pointers in c++? I mean, in c++, if I use a pointer to a byte, I can add 1 to this pointer and, in this way, move it along all the array, in c#, is there a way to do a similar thing? Thank you.

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

          Just declare a byte[] type and you can access the content in a similar way, although by index value rather than using pointer arithmetic.

          1 Reply Last reply
          0
          • M Member 7744871

            hi, does in c# exist a type of variable that is like pointers in c++? I mean, in c++, if I use a pointer to a byte, I can add 1 to this pointer and, in this way, move it along all the array, in c#, is there a way to do a similar thing? Thank you.

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            If you're using a relatively recent version of the compiler, you might want to look at the Span<T> type: Span – Adam Sitnik – .NET Performance and Reliability[^] C# 7.2: Understanding Span | Connect(); 2017 | Channel 9[^] You'll probably need to add a reference to the System.Memory NuGet package[^] to use it.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            • M Member 7744871

              hi, does in c# exist a type of variable that is like pointers in c++? I mean, in c++, if I use a pointer to a byte, I can add 1 to this pointer and, in this way, move it along all the array, in c#, is there a way to do a similar thing? Thank you.

              A Offline
              A Offline
              Anandkumar Prajapati
              wrote on last edited by
              #6

              Yes. Pointers do exist in C# Refer this link for pointers in C#: Pointers in C#

              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