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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:21:55+00:00 2026-05-27T09:21:55+00:00

As per title is there any difference passing a reference type to a method

  • 0

As per title is there any difference passing a reference type to a method such as:

public void GetStream(Stream outputStream)
{
   outputStream.Write(data);
}

VS

public Stream GetStream()
{
   MemoryStream ms = new MemoryStream();
   ms.Write(data);
   return ms; 
}

I noticed alot of Java code passes class by reference (not sure of the exact reasons for this). However in .NET is this just a matter of preference?

When would you choose one over the other?

  • 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-27T09:21:55+00:00Added an answer on May 27, 2026 at 9:21 am

    The two are very different. In your first example, the caller is responsible for creating a stream (of whatever kind they prefer), and may pass a stream that is already positioned at some arbitrary position.

    In the second, the callee has determined the type of stream, and is always writing at position 0.


    If your first example was instead:

    public void GetStream(out stream outputStream)
    {
       outputStream = new MemoryStream();
       outputStream.Write(data);
    }
    

    they would at least be closer to comparable.

    Here, the major difference is that the caller has to have a declared variable to capture the outputStream, whereas in the other case, the caller could ignore the returned stream value.

    However, the second from (returning the value) is more commonly seen – if a method has a single value to return, it’s by far preferred that that value is returned by the method, rather than a void method with an out parameter – mostly, in .NET, out parameters should be used sparingly (and only if the return value from the method is already something useful). An example of such a method would be the TryParse methods on various types, that return a bool (indicating success), and the parsed value is passed back as an out parameter.

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

Sidebar

Related Questions

As per the title, is there any way to programatically change the selected item
As per the title of the question, I’m wondering if there’s any mechanism to
As per the title, is there PHP equivalent of __name__ == __main__ ? Is
As per title, can there ever be more than 1 \ char in a
As per the title I have three parts to this question... Is db4o object
As per title, what practise for string comparisons do you use and why?
as per the title; is it possible to have nested comments in valid HTML?
As per the title, how would I fit text into a UILabel when the
As per the title, plus what are the limitations and gotchas. For example, on
As per the title, is this possible? By tri-state, I mean the parent node

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.