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

  • SEARCH
  • Home
  • 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

Related Questions

I was just made aware of a bug I introduced, the thing that surprised
I have a package that I just made and I have an old-mode that
We've got a fairly large amount of code that just made the jump to
I made some changes to an existing application that was previously just straight HTML
I just made a 100% height div on this page, but (at least in
I just made my first jQuery mobile site. I want to have people that
I just made quite a large update on a website that uses asp.net. In
I just made a project and it runs in MonoDevelop, but I would like
I just made my first converter to convert from int to string. I have
I'm using TortoiseSVN. I just made quite a few changes to my working copy

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.