In the previous article, Mytour introduced you to arrays in C#. In the following article, Mytour will further introduce you to what a string in C# is.
What is a string in C#?
Understanding Strings in C# 1. What are Strings in C#? In C#, strings are sequences of Unicode characters or arrays of characters. The Unicode character range spans from U+0000 to U+FFFF. These character arrays are referred to as text, making strings their representation.
Demystifying Strings in C# Strings in C# are often misunderstood as keywords or object classes. To gain clarity on what strings truly are in C#, delve into the following content.
Deciphering the Nature of Strings in C# Strings in C# are commonly misconceived. To understand the essence of strings in C#, delve into the following content for enlightenment.
A string is like a bouquet of characters, a masterpiece in System.String class. The keyword 'string' is just a fancy name for System.String class. Instead of typing System.String, we can simply use String, which is a shorthand for System.String class. In essence, string and String can both be aliases for System.String class. Therefore, a string is an object of System.String class.
For instance:
Let's delve into an illustration of strings in C#:
The String class is defined within the .NET class library. In other words, a String object is a collection of System.Char objects representing a string.
The maximum size of a String object in memory is 2GB or approximately 1 billion characters. The System.String class is immutable, meaning once created, its state cannot be altered.
Sample Program: Here's an illustrative example demonstrating how to declare and initialize a string in a single line.
The output result appears as follows:
2. Characteristics of Strings in C#
- It's a reference type.
- Immutable (its state cannot be changed).
- Can hold null.
- Utilizes the (==) operator.
3. Distinguishing between string and System.String
A string is an alias for System.String. Both string and System.String carry the same meaning and do not impact the application's performance. 'string' is a keyword in C#. Thus, the only difference between string and System.String depends on the context.
- String is used for declaration, while System.String is used to access static string methods.
- Strings are used to declare fields, properties, etc., utilizing the pre-defined System.String type.
- Strings must utilize class methods from System.String, such as String.SubString, String.IndexOf, etc. Strings are just aliases for System.String.
Note: In .NET, text is stored as a sequential collection of Char objects to avoid having a null character at the end of a C# string. Thus, a C# string can contain any number of embedded null characters (' ').
4. String Arrays
Additionally, we can create string arrays and assign values to them. String arrays can be created as follows:
Syntax:
String[] array_variable = new String[array_length]
Example:
Here's an illustrative example demonstrating how to create and assign values to a string array:
The output result appears as follows:
5. Reading Strings from User Input
Strings can be read from user input. The ReadLine() method of the console class is used to read a string from user input.
Example:
Input:
The output result appears as follows:
6. Various Ways to Create a String in C#
- Creating a string from a literal.
- Creating a string using string concatenation operator.
- Creating a string using constructor.
- Creating a string using properties or methods.
- Creating a string using Format.
6.1. Creating a String from Literal
This is the most common way to create a string. In this case, the user needs to define a string variable, then assign a value within double quotes. We can use any character type within double quotes except for some special characters like ().
Sample Program: Here's an illustrative example of creating a string using a literal:
The output appears as follows:
6.2. Creating Strings with String Concatenation Operator
We can create strings by using the concatenation operator '+' in C#. To create any string by combining string variables and literals, the concatenation operator (+) is used to concatenate or merge one or more strings.
Sample Program: Here's an illustrative example of creating a string using the string concatenation operator:
The output result appears as follows:
GeeksForGeeks
6.3. Creating Strings Using Constructor
The String class has been overloaded with constructors, taking an array of characters or bytes. Some constructors include pointers to arrays of characters or bytes that have been signed as parameters.
Sample Program: Here's an illustrative example of creating a string using a constructor:
The output result appears as follows:
6.4. Creating Strings Using Properties or Methods
To access a property or invoke a method always returns a string. For example, using methods of the String class (string) to extract a substring from a larger string.
Sample Program: Here's an illustrative example of creating a string using properties or methods.
The output result appears as follows:
For
6.5. Creating Strings Using Format
The Format method is used to convert a value or object to a string. The String.Format method returns a string.
Sample Program: Here's an illustrative example of creating a string using the Format method:
The output result appears as follows:
10 BMW Cars color are Red
7. Properties of String Class in C#
The String class in C# comprises 2 properties:
- Chars: Used to retrieve the Char object at the specified position in the current String object.
- Length: Used to retrieve the number of characters in the current String object.
This article by Mytour just introduced you to what a string is in C#. If you have any questions or need clarification on topics like Loops in C#, please feel free to leave your comments below the article. Mytour will address your inquiries as soon as possible.