Click here to Skip to main content
13,592,387 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi folks,

lets assume I have the following code workflow:


public void WriteToDB(string 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<string> delegate (see below)?


public void WriteToDB(Func<string> 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.

Posted 11-Dec-12 5:21am
Updated 11-Dec-12 23:10pm
Sergey Alexandrovich Kryukov 11-Dec-12 11:40am
Makes no sense at all, sorry.

1 solution

Rate this: bad
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:)
Sergey Alexandrovich Kryukov 11-Dec-12 11:42am
Exactly, a 5.
Jibesh 11-Dec-12 14:09pm
fjdiewornncalwe 11-Dec-12 11:59am
My 5
Thomas D [ProgramFOX] 11-Dec-12 12:17pm
Jens Meyer 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

CPallini 12-Dec-12 5:26am
OriginalGriff 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)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web02-2016 | 2.8.180618.1 | Last Updated 12 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

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