CopyPastehas never been so tasty!

String literals in C#

by anonymous

  • 0
  • 0
  • 0

Orignal Source:


String literals:




We can create strings in different ways. You can assign constant strings but C# provides us string literals. In string literals we have two syntax, these are classic C-like syntax and a verbatim syntax. A string literal such as @"c:\Foo" is called a verbatim string literal.


In this example we have three strings. The first one is simple string literal and second is string literal with escaped and third string literal is with the sign of @. When we run this application then we will get the output and the output will be 

This is test

This is a real test

This is a \real\ test


To demonstrate just make a console application and write the following code and run the application.









publicstaticvoid Main(string[] args)


// Create a simple string literal

string s1 = "This is a test";


// Create a string literal with an escaped character

string s2 = "This is a \"real\" test";


// Create a string literal with the @ sign

string s3 = @"This is a \real\ test";


// Output them

Console.WriteLine("String 1 = {0}", s1);

Console.WriteLine("String 2 = {0}", s2);

Console.WriteLine("String 3 = {0}", s3);





Add A Comment: