String concatenation is a feature of C# that literally means to "chain together strings". We can connect two or more strings and/or string variables to form a new string using the + operator. Here are three examples of HandleMessage method bodies that use string concatenation. Try them out for yourself:
HandleMessage method body:
return "Hello World" + " again";
Response:
Hello World again
Full chatbot source: ConcatenationExample1Bot.cs
HandleMessage method body:
string firstName = "Piston"; string lastName = "Honda"; string fullName = firstName + " " + lastName; return fullName;
Response:
Piston Honda
Full chatbot source: ConcatenationExample2Bot.cs
HandleMessage method body:
string fluffyStuff = "snow"; string compoundWord = fluffyStuff + "man"; return compoundWord;
Response:
snowman
Full chatbot source: ConcatenationExample3Bot.cs
Try this exercise: Using string concatenation, change BouncerBot.cs so that instead of just replying with the user's message, it says "You said: " and then the user's message. For example, if the bot received the message: "Hey, I know you're a bot!", it would reply:
You said: Hey, I know you're a bot!
Once you have finished this step, change the output so that it says: "Hey <user>, you said: " before the message, where <user> is the user who messaged the bot. For example, if "Don_Flamenco@passport.com" sent the message "Do you like dancing and boxing?" to the chatbot, the chatbot would respond:
Hey Don_Flamenco@passport.com, you said Do you like dancing and boxing?
To write the code for this program, you'll need to use both of your parameters. When you are done with the exercise, you can compare with this sample solution: ConcatenationExerciseBot.cs.
Previous Next