In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the "introduction to the characteristics of string in c#". In daily operation, I believe many people have doubts about the introduction to the characteristics of string in c#. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "introduction to the characteristics of string in c#"! Next, please follow the small series to learn together!
preface
String type is one of the most used types in our actual project development. String is a reference type. We all know this, but in the actual use process, we will find that string is really different from our common reference type. Look at the following simple example:
static void Main (string[] args) { string hello = "my name is yuanHong"; Console.WriteLine (string.Format("value before machining: {0}", hello)); ////Machining hello MachHello(hello); Console.WriteLine (string.Format("Processed value: {0}", hello)); Console.ReadLine(); } ////Processed hello//// private static void MachHello (string hello) { hello = string.Format("{0},Nice to meet you",hello); }
The actual result of the program is: the values before and after are the same and have not changed. If you look at the analysis according to the reference type, it should be that the values before and after processing are different. Why is that? Does it feel like strings are value types again? Now let's discuss the particularity of string.
string Introduction to Internal Implementation
First of all, string is sealed and cannot be inherited.
Secondly: through the above string underlying source code, we found that in the underlying implementation is actually implemented with char array, in initializing a string, the system has initialized the size of the char array.
string is fixed in size when it is created, and it is read-only and cannot be modified.
In practice, what we do with string actually creates a new string internally.
String when passed as a function parameter, is actually a copy of the data passed
And finally: Now that we're looking back at the most open program, it's not hard to see why it's doing this.
string Use caution
Avoid additional storage overhead
Avoid concatenating strings with + signs:
Consider the following example:
string str1 = "yuan";str1 = str1 + "hong";///This creates two strings 3 string objects string New = "yuan" + "hong";//Equivalent to strNew="yuanhong", which is actually the effect after compilation///Only one string object is created
Another example:
////Here are two ways to return a string 123////way 1 string v11="1";string v22=v11+"2";string v33=v22+"3";retun v33;///way 1: the system creates five string objects///way 2///way 2: the system only creates four string objects string v1="1";string v2="2";string v3="3"; retun v1+v2+v3;///In terms of memory overhead, obviously way 2 is better than way 1
In practice, StringBuilder is recommended for frequent concatenation of string objects.
Of course, c#also has a simplified string concatenation method: String.Format , in fact, its internal implementation principle is StringBuilder
2. Minimize packing
Direct code example:
string str1 = "yunqiong" + 66;string str2 = "yunqiong" + 66.ToString();///View the compiled code and find that the first line of code requires a boxing operation. Boxing operation requires unnecessary memory overhead. First, memory needs to be distributed for the value type itself, and memory overhead needs to be allocated for the type pointer and the same block index.
Summary:
In actual development, we need to pay attention to several points:
1. Avoid packing operations
2. Avoid using + to concatenate strings
At this point, the study of "introduction to the characteristics of string in c#" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.