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 8332203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:39:21+00:00 2026-06-09T02:39:21+00:00

I created a method that takes 2 out parameters. I noticed that it is

  • 0

I created a method that takes 2 out parameters. I noticed that it is possible for calling code to pass in the same variable for both parameters, but this method requires that these parameters be separate. I came up with what I think is the best way to validate that this is true, but I am unsure if it will work 100% of the time. Here is the code I came up with, with questions embedded.

private static void callTwoOuts()
{
    int same = 0;
    twoOuts(out same, out same);

    Console.WriteLine(same); // "2"
}

private static void twoOuts(out int one, out int two)
{
    unsafe
    {
        // Is the following line guaranteed atomic so that it will always work?
        // Or could the GC move 'same' to a different address between statements?
        fixed (int* oneAddr = &one, twoAddr = &two)
        {
            if (oneAddr == twoAddr)
            {
                throw new ArgumentException("one and two must be seperate variables!");
            }
        }

        // Does this help?
        GC.KeepAlive(one);
        GC.KeepAlive(two);
    }

    one = 1;
    two = 2;
    // Assume more complicated code the requires one/two be seperate
}

I know that an easier way to solve this problem would simply be to use method-local variables and only copy to the out parameters at the end, but I am curious if there is an easy way to validate the addresses such that this is not required.

  • 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-06-09T02:39:22+00:00Added an answer on June 9, 2026 at 2:39 am

    I’m not sure why you ever would want to know it, but here’s a possible hack:

    private static void AreSameParameter(out int one, out int two)
    {
        one = 1;
        two = 1;
        one = 2;
        if (two == 2)
            Console.WriteLine("Same");
        else
            Console.WriteLine("Different");
    }
    
    static void Main(string[] args)
    {
        int a;
        int b;
        AreSameParameter(out a, out a); // Same
        AreSameParameter(out a, out b); // Different
        Console.ReadLine();
    }
    

    Initially I have to set both variables to any value. Then setting one variable to a different value: if the other variable was also changed, then they both point to the same variable.

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

Sidebar

Related Questions

Trying to figure out how to create a method that takes in a list
I can't fugure out how to pass an anonymous function that takes a parameter.
I want to create a method that takes two variables timeIn and timeOut and
I would like to create a method that takes an event as an argument
I have created a method that should ideally take a single Account object and
I am trying to create a unit test for a method that takes a
I would like to create an NSInvocation object using a method that takes a
I created a simple method that deletes an image from the server. public static
I have created a custom method that will return unique items, together with the
I created an NSScanner category method that shows a leak in instruments. - (BOOL)scanBetweenPrefix:(NSString

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.