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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:47:36+00:00 2026-06-02T19:47:36+00:00

Can I use an out parameter with a recursive method? If it’s possible, how

  • 0

Can I use an out parameter with a recursive method? If it’s possible, how can I do it with the following code?

private void PrepareDir(out List<_File> listFiles,string root,string dirPath) {
    DirectoryInfo dirRoot = new DirectoryInfo(dirPath);
    FileInfo [] Files = dirRoot.GetFiles();
    dirPath = dirPath.Substring(root.Length);

    foreach (FileInfo file in Files) {
        _File _file = new _File();
        _file.Name = dirPath + "\\" + file.Name;
        _file.Path = file.FullName;
        _file.Size = file.Length;
        listFiles.Add(_file);
    }

    foreach (DirectoryInfo dir in dirRoot.GetDirectories()) {
        PrepareDir(out listFiles, root, dir.FullName);
    }
}

private void btnButton1_Click(object sender, EventArgs e) {
    List<_File> Files = new List<_File>();
    PrepareDir(out Files,currAddress, currAddress);
}
  • 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-02T19:47:37+00:00Added an answer on June 2, 2026 at 7:47 pm

    I decided to rewrite my answer to be more direct about why using out is not necessary here. I wrote this to be rather lengthy, because I think the root of your question is more in not understanding the differences between passing by value, passing by reference, and the common confusion between a reference type being passed by reference. Also note that this isn’t explicit to only recursion.

    In C#, reference types are passed by value by default. A lot of people can confuse a reference type with passing by reference, but there is an important difference. To make myself more clear, I will refer to a reference type as is, and passing by reference as using the ref or out keywords.

    Passing a reference type by value, because it is a reference to some storage location in memory, allows you to make and persist changes. Take this example.

    public class MyRefType
    {
        public int Value { get; set; }
    }
    
    public void Foo() {
        MyRefType type = new MyRefType();
        AddOne(type);
    }
    
    public void AddOne(MyRefType type) {
        type.Value++;
    }
    

    What will happen here, is that the class type will now have a value of one. This is the nature of a reference type. If type were a struct, it would still have a value of 0 inside the Foo method, because a copy was made instead of holding a reference to the object.

    Now that I hope you understand the symantics of passing a reference type by value, let’s actually talk about passing a reference type by reference. This is done using the out and ref keywords. Make it a point to note immediately that in the CLR, out technically does not exist, only ref does. out is actually represented in metadata as ref with a special attribute applied to it, and they ought to be treated the same.

    Now let’s say we wanted to re-assign our reference type in the AddOne method before doing the addition.

    public class MyRefType
    {
        public int Value { get; set; }
    }
    
    public void Foo() {
        MyRefType type = new MyRefType();
        AddOne(type);
    }
    
    public void AddOne(MyRefType type) {
        type = new MyReferenceType();
        type.Value++;
    }
    

    Because we are still passing our reference type by value, the value of type in the method Foo will still be 0. All we did was initialize another storage location in memory, instead of reassigning the original storage location). But we want to actually pass our reference type by reference, so we should really do:

    public class MyRefType
    {
        public int Value { get; set; }
    }
    
    public void Foo() {
        MyRefType type = new MyRefType();
        AddOne(ref type);
    }
    
    public void AddOne(ref MyRefType type) {
        type = new MyReferenceType();
        type.Value++;
    }
    

    Now the value of type in the Foo method will be 1. This is because we reused the same storage location that the reference pointed to, instead of creating a new location in memory.

    So how does all this apply to out? Remember that out is the same as ref with different usage symantics. The difference is that with ref you may leave the method without the reference being initialized, whereas with out, you must explicitly initialize it before the method returns.

    So in your case of using out here, it is completely not necessary because you already have existing reference symantics working for you. I hope this clears up a bit.

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

Sidebar

Related Questions

I can use ipcs(1) to list out the active shared memory objects on a
I can't figure out the use for this code . Of what use is
The following question and answer addresses the use of an out parameter in a
I can use stat() to figure out what permissions the owner, group, or others
I found out that I can use a different theme in an C# WPF
In *nix systems one can use which to find out the full path to
Is there any library out there that I can use to create epub files
For laying out a long string into pages, I can use CTFramesetterCreateFrame to create
Solution Edit: Turns out you can't use the PHP SDK to return the correct
how can i use jQuery to show/hide the same div area... essentially swapping out

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.