Click here to Skip to main content
15,891,708 members
Home / Discussions / C#
   

C#

 
GeneralRe: Insert variable into string Pin
Rhys Jacob29-Jun-10 2:52
Rhys Jacob29-Jun-10 2:52 
GeneralRe: Insert variable into string Pin
Keith Barrow29-Jun-10 4:13
professionalKeith Barrow29-Jun-10 4:13 
GeneralRe: Insert variable into string Pin
William Winner29-Jun-10 8:47
William Winner29-Jun-10 8:47 
GeneralRe: Insert variable into string Pin
Keith Barrow29-Jun-10 11:04
professionalKeith Barrow29-Jun-10 11:04 
GeneralRe: Insert variable into string Pin
William Winner29-Jun-10 11:17
William Winner29-Jun-10 11:17 
GeneralRe: Insert variable into string Pin
kevinnicol29-Jun-10 7:08
kevinnicol29-Jun-10 7:08 
GeneralRe: Insert variable into string Pin
PIEBALDconsult29-Jun-10 14:36
mvePIEBALDconsult29-Jun-10 14:36 
GeneralRe: Insert variable into string Pin
William Winner29-Jun-10 8:12
William Winner29-Jun-10 8:12 
I'm just curious about something here. The header for String.Format looks like:

C#
string Format(string strInput, object objInput)


right? So, right away, a new string is created, along with a reference to the object.

Now, the code, would have to first find the starting and ending brace and pull out the formatting codes. Then, it would have to take the object and apply the formatting code to it and then return the part of the string before the brace, the formatted code, and the part of the string after the brace. In other words, if you had passed it "format {0} me", the return would look like:

C#
return left(strInput, startingBraceIndex) & Format(objInput, formatCode) & right(strInput, strInput.Length - endingBraceIndex - 1)


or something like that. Of course, it is (hopefully) optimized a bit, so that it's not creating as many copies, but still, wouldn't more memory have to be used for the Format method, plus more processing cycles because or what all has to be done...whereas the second routine is much more straightforward.

As far as performance, I just don't see how the first is more efficient than the second. I also don't see how the first is more readable...even with more parameters, you would have to know what was in each spot in the array, so as far as readability, the second would be more readable as well.

Is

C#
object[] objArray = {devNr, idNr, testNr};
return string.Format("Device #{0}, ID#{1}, Test#{2}", objArry);


really more readable than:
C#
return "Device #" + devNr + ", ID#" + idNr + ", Test#" + testNr
?

Sure the first one is a bit cleaner, but IMO, it's not more readable.

Besides, whatever happened to Occam's Razor?
GeneralRe: Insert variable into string Pin
Łukasz Nowakowski29-Jun-10 8:23
Łukasz Nowakowski29-Jun-10 8:23 
AnswerRe: Insert variable into string Pin
yu-jian30-Jun-10 19:19
yu-jian30-Jun-10 19:19 
QuestionSysListView32 another application Pin
iceeeeman28-Jun-10 21:36
iceeeeman28-Jun-10 21:36 
AnswerRe: SysListView32 another application Pin
Richard MacCutchan28-Jun-10 21:39
mveRichard MacCutchan28-Jun-10 21:39 
GeneralRe: SysListView32 another application Pin
iceeeeman28-Jun-10 21:50
iceeeeman28-Jun-10 21:50 
GeneralRe: SysListView32 another application Pin
Richard MacCutchan29-Jun-10 2:36
mveRichard MacCutchan29-Jun-10 2:36 
GeneralRe: SysListView32 another application Pin
iceeeeman29-Jun-10 6:05
iceeeeman29-Jun-10 6:05 
GeneralRe: SysListView32 another application Pin
Richard MacCutchan29-Jun-10 6:45
mveRichard MacCutchan29-Jun-10 6:45 
QuestionCustom Session Pin
satsumatable28-Jun-10 20:01
satsumatable28-Jun-10 20:01 
AnswerRe: Custom Session Pin
Ennis Ray Lynch, Jr.29-Jun-10 5:38
Ennis Ray Lynch, Jr.29-Jun-10 5:38 
QuestionQestion about FieldInfo[] reflection Pin
andreas0428-Jun-10 19:51
andreas0428-Jun-10 19:51 
AnswerRe: Qestion about FieldInfo[] reflection Pin
Nish Nishant29-Jun-10 3:19
sitebuilderNish Nishant29-Jun-10 3:19 
GeneralRe: Qestion about FieldInfo[] reflection Pin
andreas0429-Jun-10 4:15
andreas0429-Jun-10 4:15 
GeneralRe: Qestion about FieldInfo[] reflection Pin
Nish Nishant29-Jun-10 4:18
sitebuilderNish Nishant29-Jun-10 4:18 
GeneralRe: Qestion about FieldInfo[] reflection Pin
Nish Nishant29-Jun-10 4:22
sitebuilderNish Nishant29-Jun-10 4:22 
GeneralRe: Qestion about FieldInfo[] reflection Pin
andreas041-Jul-10 5:32
andreas041-Jul-10 5:32 
QuestionCloning Controls Within A Library Project - Found A Solution (no pun intended) Pin
Roger Wright28-Jun-10 17:40
professionalRoger Wright28-Jun-10 17:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.