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)
{
}
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<string> 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)
{
}
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