-1

I have this code for a virtual directory class:

    public class VirtualDirectory
    {

        internal Dictionary<string, VirtualDirectory> directories;
        internal Dictionary<string, VirtualFile> files;

        /// <summary>
        /// Gets the specified directory.
        /// </summary>
        /// <returns>A reference to the specified directory.</returns>
        public VirtualDirectory getDirectory(string name)
        {
            return directories[name];
        }

        /// <summary>
        /// Adds a file to the directory.
        /// </summary>
        /// <param name="file">The file to be added.</param>
        /// <param name="name">The file's name.</param>
        public void addFile(VirtualFile file, string name)
        {
            files.Add(name, file);
        }
    }

If I do this:

// Say this new directory is populated
VirtualDirectory directory = new VirtualDirectory;
directory.getDirectory("exdir").addFile(new VirtualFile(), "exfile");

Is the original directory (directory.directories["exdir"]) modified? Or is it just the instance of the directory "exdir"?

austanss
  • 271
  • 3
  • 9
  • Your code cannot compile, and even if it did, there are not sufficient details to know exactly what it's doing. Also, it's not even clear that there _is_ a difference between `directory.directories["exdir"]` and _"the instance of the directory "exdir""_, making your question unclear. That said, there is already a wealth of information available about what reference types are and how they work. E.g. see marked duplicate. Between all of the documentation, as well as simply executing your code in a debugger and monitoring what happens, you should be able to resolve for yourself the answer. – Peter Duniho Jul 11 '20 at 16:38
  • @PeterDuniho marking as duplicate didn't answer my question. I KNOW what reference and value types are, I was asking in this case if I was using value types or reference types. *"the instance of the directory exdir"* refers to the return value of `getDirectory()` – austanss Jul 11 '20 at 17:06

1 Answers1

0

Writing:

directory.getDirectory("exdir").addFile(new VirtualFile(), "exfile");

Is like:

var dir = directory.getDirectory("exdir");
dir.addFile(new VirtualFile(), "exfile");

Thus you don't modify the directory instance you got, that said you don't modify the reference, but you add an item in that collection, thus the collection content itself is modified.

Therefore there is a new file in the directory.directories["exdir"].

You can have multiple reference to only one instance of a class and using one of them to manipulate this instance provides the same result as using another.

So here the dir variable is a reference to the instance of directory.directories["exdir"] that is also accessible using this reference directly.

Value Type and Reference Type

Difference between a Value Type and a Reference Type

  • _"Thus you don't modify the directories item you got, but you add an item in that collection"_ -- I don't see how you can claim that. The `VirtualDirectory` object being modified is a completely different one from the `directory` object. There are at least two collections here, and you are not clear at all about which one you're talking about. This answer doesn't make any sense. – Peter Duniho Jul 11 '20 at 16:41
  • So what? There's nothing in the question asking whether a _reference_ is modified. – Peter Duniho Jul 11 '20 at 16:43