Home > Article > Backend Development > String literals vs. string objects in C#
String literals or constants are enclosed in double quotes "" or @"". Strings contain characters similar to character literals: plain characters, escape sequences, and universal characters.
The following are some examples of string literals -
Hello, World" "Welcome, \
The following are examples showing the usage of string literals -
using System; namespace Demo { class Program { static void Main(string[] args) { // string string str1 ="Hello, World"; Console.WriteLine(str1); // Multi-line string string str2 = @"Welcome, Hope you are doing great!"; Console.WriteLine(str2); } } }
Create a string object using one of the following methods -
Here's how to create a string object and compare two strings -
using System; namespace Demo { class Program { static void Main(string[] args) { string str1 = "John"; string str2 = "Andy"; if (String.Compare(str1, str2) == 0) { Console.WriteLine(str1 + " and " + str2 + " are equal strings."); } else { Console.WriteLine(str1 + " and " + str2 + " are not equal strings."); } Console.ReadKey() ; } } }
The above is the detailed content of String literals vs. string objects in C#. For more information, please follow other related articles on the PHP Chinese website!