In any programming language, to represent the value of a variable, we need to declare the data type of that variable.
In previous lessons, Bờm reviewed value types and reference types. Following that series, Bờm wrote something about a data type that we also use a lot to process strings, which is String. So what do we know about string type in C# language? With limited knowledge, I will only point out a few basic points about strings as a way to review.
Yeah, let's go:
1. What is String? string and System.String are the same or different?
For string processing .Net framework provides us with a class: System.String (fields, methods, constructor...). A String in C# is an object of type String, its value is text. This means that if you want to store information of a string, a text, you can completely use string type to store.
// String of characters
System.String nickName = "Victor";
// String made of an Integer
System.String age = "28";
Many of you also asked if string and System.String is different or the same, oh good question... When I was a student, I used to ask the same question... And the answer is: in In C#, the keyword string is an alias of String, so the two notes string and String are basically the same. In programming you want to use string or String keyword, it's up to you!!!
2. String is an immutable.
This means you can't change the string once it's created.
For example:
string string1 = "Hello World!!";
It will occupy a memory area on the heap to store this string.
If you want to change the value of string1 by adding to string1 the value "from Tutorials teacher". You think: the system just needs to change the value of string1 by adding the value "from Tutorials teacher" and it's ok...
Oh No, because of its immutable nature, it will create a new string "Hello!! from tutorials teacher" and assign this value to string1.
Unprecedented magic and this magic brings a disadvantage that if we modify the string1 many times, it also creates many new strings, result in reduced program performance.
To solve this problem, C# martial arts secrets give us a spell String "Biu Do".
3. String Builder
String builder helps to increase performance when working with string type when we need to edit, delete, or any operation on string. It increases performance because each time it manipulates a string, it doesn't create any new strings, simply manipulates the string itself.
(image: stringbuilder_memory; source: tutorialteacher.com)
Let see the below example:
Yup!! Here's the result, surprise!!!
In short, string builder is useful when working with string concatenation and string operations, it can increase performance when manipulating strings.
When working on strings, we need to call methods or apply operations on them. So what happens when our string variable has a value of null.
4. Null string in C#
A null string is a variable of type string but it is uninitialized or has a value of null.
If we call a property or a method on a null string, boom.... an exception will be thrown... This makes us think, to get the best handling code. A small note, but martial.
The above is just a little basic theory about string so that we have the concept of null string, what is an immutable string? how to solve performance problems when working with strings... There are many things about strings, such as helper methods, properties, fields... I think the basic knowledge of what string is, string is immutable it needs to be stored in our memory before we find a way to apply it to real string processing problems.
Related Posts:




