Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.5 C#
Hi folks,

lets assume I have the following code workflow:

string s = "SOME_LONG_AND_NASTY_STRING_PROBABLY_A_BASE64_STRING";
WriteToDB(s);
 
public void WriteToDB(string str){
    InsertInDB(str);
}
 
public void InsertInDB(string str)
{
    //Do writing here using str
}

Since the string is a value type immutable each method call with it as parameter leads to a copy of that string in the memory. Even if WriteToDB justs routes the string to InsertInDB a copy is created. To minimize this bad effect I can wrap it in some data container and pass the container around. The container is of course a reference type.

What if I wrap the string in a Func delegate (see below)?

string s = "SOME_LONG_AND_NASTY_STRING_PROBABLY_A_BASE64_STRING";
WriteToDB(()=>s);
 
public void WriteToDB(Func<string> f_str){
    InsertInDB(f_str);
}
 
public void InsertInDB(Func<string> f_str)
{
    //Do writing here using f_str()
}

Is it true that the string is wrapped inside the Func-object and therefore passed as a reference type just like if i put it in a data container?

Id like to prevent myself to write a data container just to hold the string. Is there a better way to have a string passed as a parameter but without having it copied all the time? I know the ref keyword, but that seems to be wrong either in this case.

Thanks for your suggestions.

Jens
Posted 11-Dec-12 6:21am
Edited 12-Dec-12 0:10am
v2
Comments
Sergey Alexandrovich Kryukov at 11-Dec-12 11:40am
   
Makes no sense at all, sorry.
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Since when was a string a value type?
Trust me on this, all .NET strings are reference types, so your concern is immaterial. (As is your question Laugh | :laugh: )
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 11-Dec-12 11:42am
   
Exactly, a 5.
--SA
Marcus Kramer at 11-Dec-12 11:59am
   
My 5
ProgramFOX at 11-Dec-12 12:17pm
   
+5!
jibesh at 11-Dec-12 14:09pm
   
true!
Jens Meyer at 12-Dec-12 5:09am
   
Ok, thanks for your answers. I confused reference type/value type with types beeing immutable. So keeping immutable instead of value type in mind my question stays the same. I edited the original question.

Please give me any further advice on this.

Thanks and regards

Jens
OriginalGriff at 12-Dec-12 5:17am
   
The question doesn't change, because the mutability of the string is also irrelevant here. A reference is a reference, regardless of whether it is mutable or not - what ever you do to pass it, all you do is pass the reference (effectively a 32 or 64 bit number). The string data itself is only ever copied when you try to change it (that's what the immutability is all about: when you try to change a string a new instance is created as a copy of the original and the modification happens to that, producing a new, different reference)
CPallini at 12-Dec-12 5:26am
   
:-D
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 255
1 Suvendu Shekhar Giri 215
2 Andy Lanng 210
3 PIEBALDconsult 190
4 Abhinav S 184
0 Sergey Alexandrovich Kryukov 6,310
1 OriginalGriff 5,853
2 Peter Leow 2,484
3 Maciej Los 2,263
4 Abhinav S 2,249


Advertise | Privacy | Mobile
Web02 | 2.8.150414.1 | Last Updated 12 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100