Click here to Skip to main content
15,912,329 members
Home / Discussions / C#
   

C#

 
AnswerRe: String Concatenation Pin
N a v a n e e t h30-Dec-08 23:22
N a v a n e e t h30-Dec-08 23:22 
AnswerRe: String Concatenation Pin
nelsonpaixao31-Dec-08 4:02
nelsonpaixao31-Dec-08 4:02 
AnswerRe: String Concatenation Pin
PIEBALDconsult31-Dec-08 4:20
mvePIEBALDconsult31-Dec-08 4:20 
AnswerRe: String Concatenation Pin
#realJSOP31-Dec-08 4:37
professional#realJSOP31-Dec-08 4:37 
GeneralRe: String Concatenation Pin
Guffa1-Jan-09 2:43
Guffa1-Jan-09 2:43 
GeneralRe: String Concatenation Pin
#realJSOP2-Jan-09 8:59
professional#realJSOP2-Jan-09 8:59 
AnswerRe: String Concatenation Pin
Ravi Bhavnani31-Dec-08 7:25
professionalRavi Bhavnani31-Dec-08 7:25 
AnswerRe: String Concatenation Pin
Guffa31-Dec-08 16:12
Guffa31-Dec-08 16:12 
Your method 1 is absolutely the most efficient. It concatenates the strings while compiling, so the only work done at runtime is allocating a reference of an already existing string to the variable.

I suppose that you meant to concatenate string values from variables, not literal strings, so there is actually something done at runtime:

Method 1:
string key = str1 + "::" + str2;

Method 2:
string key = string.Format("{0}::{1}", str1, str2);

Method 1 is still the most efficient. It will compile into a call to the String.Concat(string, string, string) method, which adds the lengths of the strings, allocates a string with that size, and copies the data from the strings into the new string.

Method 2 uses a StringBuilder, which is overkill when you just want to concatenate three strings. It calls the AppendFormat method which has to parse the format string to know what to do with the strings.

Using the String.Format method often produces more readable code, and it can certainly do a lot more than just concatenating strings, but for your specific question it's not the most efficient.

Despite everything, the person most likely to be fooling you next is yourself.

modified on Thursday, January 1, 2009 9:04 AM

Questionplz help me about this Algo Pin
wasimsharp30-Dec-08 22:00
wasimsharp30-Dec-08 22:00 
AnswerRe: plz help me about this Algo Pin
Eddy Vluggen2-Jan-09 2:15
professionalEddy Vluggen2-Jan-09 2:15 
QuestionRead From file data till reach ‘\0’ Pin
Ronenb30-Dec-08 20:39
Ronenb30-Dec-08 20:39 
AnswerRe: Read From file data till reach ‘\0’ Pin
Lev Danielyan30-Dec-08 20:44
Lev Danielyan30-Dec-08 20:44 
AnswerRe: Read From file data till reach ‘\0’ [modified] Pin
PIEBALDconsult31-Dec-08 4:43
mvePIEBALDconsult31-Dec-08 4:43 
AnswerRe: Read From file data till reach ‘\0’ Pin
PIEBALDconsult31-Dec-08 6:06
mvePIEBALDconsult31-Dec-08 6:06 
GeneralRe: Read From file data till reach ‘\0’ Pin
Ronenb31-Dec-08 19:10
Ronenb31-Dec-08 19:10 
Questionhow to get index in checkboxlist control by passing data value membet Pin
ademsandeepreddy30-Dec-08 20:14
ademsandeepreddy30-Dec-08 20:14 
QuestionLocking Screen! Pin
spiritboy30-Dec-08 19:59
spiritboy30-Dec-08 19:59 
AnswerRe: Locking Screen! Pin
Giorgi Dalakishvili30-Dec-08 20:11
mentorGiorgi Dalakishvili30-Dec-08 20:11 
GeneralRe: Locking Screen! Pin
Michael Bookatz31-Dec-08 1:55
Michael Bookatz31-Dec-08 1:55 
GeneralRe: Locking Screen! Pin
Giorgi Dalakishvili31-Dec-08 4:07
mentorGiorgi Dalakishvili31-Dec-08 4:07 
AnswerRe: Locking Screen! Pin
vaghelabhavesh31-Dec-08 8:16
vaghelabhavesh31-Dec-08 8:16 
Questionhow to write code for browse button Pin
NarVish30-Dec-08 19:54
NarVish30-Dec-08 19:54 
AnswerRe: how to write code for browse button Pin
Giorgi Dalakishvili30-Dec-08 20:12
mentorGiorgi Dalakishvili30-Dec-08 20:12 
AnswerRe: how to write code for browse button Pin
ashok104230-Dec-08 23:37
ashok104230-Dec-08 23:37 
GeneralRe: how to write code for browse button Pin
Not Active31-Dec-08 5:42
mentorNot Active31-Dec-08 5:42 

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.