Declaring, Creating, and Initializing a String in C
Declaring a String Variable
To declare a string variable in C#, use the following syntax:
- This tells the compiler that
myString
is a string variable. - At this point,
myString
is uninitialized and has a default value ofnull
.
Note
- A variable with a
null
value means it is not pointing to any object in memory. Trying to usemyString
without assigning a value will result in a runtime error.
Understanding Reference Types
Strings, like arrays, are reference types. This means:
- Stack vs. Heap Memory:
- The variable
myString
on the stack holds a reference (memory address) to the actual string object stored on the heap. - When you declare a string without assigning a value, the variable exists but does not reference any object in memory.
- The variable
Instantiating and Initializing a String
Before using a string, you must initialize it by assigning a value:
- The string literal
"Hello, World!"
is created in the heap, andmyString
holds a reference to this location. name
is initialized as an empty string (""
).
Warning
- Declaring a string variable without assigning a value and then trying to use it will result in a
NullReferenceException
.
Copying String References
If you assign one string variable to another, you're copying the reference, not the value:
- This means
myNewString
now points to the same memory location asmyString
. - Any changes to the string object will be reflected in both variables.
Note
- Even though strings are reference types, they are immutable. This means you cannot change the value of an existing string object in memory. Any modification results in a new string object being created.
String Initialization in One Step
You can declare and initialize a string in a single step:
Reading and Printing Strings
We have seen these methods quite a bit in previous sections, but copied here for completeness.
Reading from the Console
The ReadLine()
method reads a line of text entered by the user and returns it as a string:
- The
ReadLine()
method waits for the user to press Enter and assigns the input to thename
variable.
Printing to the Console
The WriteLine()
method prints a string to the console:
- This uses string concatenation to combine the greeting and the
name
variable.
String Interpolation
String interpolation provides a more readable way to include variables in strings:
- The
$
symbol before the string allows you to embed variables directly within the curly braces{}
.
Advantages of String Interpolation:
- More readable and easier to manage, especially with complex strings.
- Avoids the need to use the +
operator for concatenation.