
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
String Literal Vs String Object in C#
String Literals
String literals or constants are enclosed in double quotes "" or with @"". A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.
Here are some examples of String Literals −
Hello, World" "Welcome, \
The following is an example showing the usage of string literals −
Example
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); } } }
String Object
Create string object using one of the following methods −
- By assigning a string literal to a String variable
- By using a String class constructor
- By using the string concatenation operator (+)
- By retrieving a property or calling a method that returns a string
- By calling a formatting method to convert a value or an object to its string representation
The following is how you can create a string object and compare two strings −
Example
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() ; } } }
- Related Articles
- Raw string literal in C++
- Raw string literal in C++ program
- What is a string literal in C++?
- What is the difference between a String object and a String literal in Java?
- How to define multiline String Literal in C#?
- What is Java String literal?
- Why String literal is stored in String Constant Pool in Java?
- char vs string keywords in C#
- JavaScript: How to Check if a String is a Literal or an Object?
- Character constants vs String literals in C#
- PHP $string{0} vs. $string[0];
- How to use template string literal in ECMAScript 6?
- puts() vs printf() for printing a string in C language
- How to create a String object in C#?
- Replace value with a string literal during MongoDB aggregation operation

Advertisements