We help IT Professionals succeed at work.

Passing a Sting in memory from one Object to another.

Mr_Fulano used Ask the Experts™
Hi, I'm using MS VS 2010 C#.NET. This is somewhat of an academic question for my edification. I know there are other (and probably better) ways to do this.

I'm using the StringBuilder method to read a file from disk. Once that file is read, I was trying to pass the file's content, which is now in memory (a/k/a the "String" in memory) from the StringBuilder to the StreamReader object, so I can use some of SteamReader's BaseStream properties like Position and Length, etc.

I know that's possible, because I tried it, and it worked, but is it -- "advisable" to do that?

I figured that once the sting is in memory, it can be used by any object (as long as its technically possible). Or is there some sort of ownership that the SringBuilder places on the string it created, that would make its an undesirable practice to switch objects handling the string?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Miguel OzSenior Software Engineer
Top Expert 2009

I would not recommend to have two objects in memory representing the same content (file contents)
This link shows the typical contruct to access a file using streamreader:
As you can see no need to build an extra step (string builder object) unless required as part of your logic. For example you use the string builder contents in other part of your logic and even in that case I will use the streamreader to build the contents of the StringBuilder object and drive whatever stream reader logic  your code has.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Hi Mr_Fulano,

Very nice approach towards learning. Plus this is C# and it does work in a different way then C/C++ so you don't have to worry about passing it across the objects as long as you want them to access it.

Now to answer the question, No it does not put any kind of ownership but we have a direct way to read a file into a stream
For example,



Thank you both for the input and good advice.

Point of clarification: I misspoke calling the StreamBuilder and StreamReader "Method" rather than "Classes", which is really what they are. So, please excuse any confusion that may have caused.

That said, this was an interesting exercise. I wanted to see if I could pass one object (a string) off from one Class to another. Theoretically, there should be no issues. However, we all know that practice and theory are family, but not identical twins, so I wanted to test my theory and seek the advice of those more experienced that I.

Thanks again,
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018
That's alright. Basically anything and everything can be passed around as long as you know how to apply a cast and as long as the object is not protected with restrictive access modifiers[private especially]. Also note that receiving end needs to 'understand' what type of object you have passed(upto an extent - as C# advances it is accomodating a lot of new features - check out keywords : dynamic and var)
Senior Software Engineer
Top Expert 2009
You can pass any objects from one class to another, but remember that for the string scenario, the string in.net are inmutable objects.
In other words,  if the string gets modified inside another class - the CLR will create a copy of your string, thus the new string do have a different reference to the original string.
For more details, check:


Thank you both. Very good information.