Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 290541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:00:25+00:00 2026-05-12T06:00:25+00:00

I just made a Swap routine in C# like this: static void Swap(ref int

  • 0

I just made a Swap routine in C# like this:

static void Swap(ref int x, ref int y)
{
    int temp = x;
    x = y;
    y = temp;
}

It does the same thing that this C++ code does:

void swap(int *d1, int *d2)
{
    int temp=*d1;
    *d1=*d2;
    *d2=temp;
}

So are the ref and out keywords like pointers for C# without using unsafe code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:00:26+00:00Added an answer on May 12, 2026 at 6:00 am

    They’re more limited. You can say ++ on a pointer, but not on a ref or out.


    EDIT Some confusion in the comments, so to be absolutely clear: the point here is to compare with the capabilities of pointers. You can’t perform the same operation as ptr++ on a ref/out, i.e. make it address an adjacent location in memory. It’s true (but irrelevant here) that you can perform the equivalent of (*ptr)++, but that would be to compare it with the capabilities of values, not pointers.


    It’s a safe bet that they are internally just pointers, because the stack doesn’t get moved and C# is carefully organised so that ref and out always refer to an active region of the stack.


    EDIT To be absolutely clear again (if it wasn’t already clear from the example below), the point here is not that ref/out can only point to the stack. It’s that when it points to the stack, it is guaranteed by the language rules not to become a dangling pointer. This guarantee is necessary (and relevant/interesting here) because the stack just discards information in accordance with method call exits, with no checks to ensure that any referrers still exist.

    Conversely when ref/out refers to objects in the GC heap it’s no surprise that those objects are able to be kept alive as long as necessary: the GC heap is designed precisely for the purpose of retaining objects for any length of time required by their referrers, and provides pinning (see example below) to support situations where the object must not be moved by GC compacting.


    If you ever play with interop in unsafe code, you will find that ref is very closely related to pointers. For example, if a COM interface is declared like this:

    HRESULT Write(BYTE *pBuffer, UINT size);
    

    The interop assembly will turn it into this:

    void Write(ref byte pBuffer, uint size);
    

    And you can do this to call it (I believe the COM interop stuff takes care of pinning the array):

    byte[] b = new byte[1000];
    obj.Write(ref b[0], b.Length);
    

    In other words, ref to the first byte gets you access to all of it; it’s apparently a pointer to the first byte.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 147k
  • Answers 147k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could always add a boolean on your ViewModel and… May 12, 2026 at 9:14 am
  • Editorial Team
    Editorial Team added an answer just wondering ... why don't, if you're using C#, not… May 12, 2026 at 9:14 am
  • Editorial Team
    Editorial Team added an answer The /publish option of MSTest.exe does a license check to… May 12, 2026 at 9:14 am

Related Questions

I'd like to get all the permutations of swapped characters pairs of a string.
I just made a browser using c# as one of my first projects of
Could anybody give an example when xsl:foreach is really useful? I just made a
I have before created a quick Ajax Framework for one of my projects. It
Is there some way to replicate rails' link-to-unless-current? Ie. if i have a list

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.