Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Special application scenario analysis of string invariance and equality judgment in .net

2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of the special application scenario analysis of string invariance and equality judgment in .net, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe that you will gain something after reading this special application scenario analysis article of string invariance and equality judgment in .net. Let's take a look.

problem

Please look at the following example and answer the question.

Var S1 = "12"; var S2 = "12"; / / serialization mode 1var o3 = Newtonsoft.Json.JsonConvert.DeserializeObject (Newtonsoft.Json.JsonConvert.SerializeObject (S1)); / / serialization mode 2MemoryStream stream = new MemoryStream (); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter (); bf.Serialize (stream, S1); stream.Seek (0, SeekOrigin.Begin); var o4 = bf.Deserialize (stream) / / = split line = var e1 = object.ReferenceEquals (S1, S2); var e2 = o4 = = S1 domestic var e3 = s1.Equals (o4); var e4 = o3 = = o4ten Console.ReadKey ()

Please answer whether the values of e1, e2, e3 and e4 are true or false after the split line.

Everyone knows that strings are a classic example of shared meta-patterns in .net. The string is invariant. (at least at the managed level, you can actually change the value of the string at the unmanaged layer), but can you really answer the above question?

Answer

E1 = true

E2 = false

E3 = true

E4 = false

To understand this problem, you can first look at the layout of strings in memory.

How to view the memory layout of variables in visual studio

You can easily view the memory values of managed or unmanaged variables in VS. The method is as follows.

Open the memory dialog box in debug mode-> window-> memory-> memory 1 (either 1 or 4).

Just enter the variable name in the address bar.

Layout of string variables in memory

In .net, strings are saved in memory in UTF-16 format. In this example, the memory of S1 is as follows.

00 00 00 98 d6 fc e5 fb 7f 00 00 02 00 00 00 31 00 32 00

This may not be the same as the result you got. You may not have the top 8 0x00, because I put the object's head on. The meaning of each paragraph is explained in turn below.

00 00 00 the first 8 bits are the object head. Among them, under 64-bit, the high 4 bits is 0, and the low 4 bits is a number that is not 0. (since no lock or Gethashcode operation is performed here, it is 0 here. If you are interested in self-experiment, you are interested.)

98 d6 fc e5 fb 7f 00 object MethodTable, depending on the type, the location that the object's reference points to.

02 00 00 00 string length, here is 2.

31 00 32 00 string array * char, note that they are all in small-end mode.

Using the above S1 / S2 / 3o4, we can find that their memory is exactly the same, in which S1 / S2 is directly the same memory address, but the remaining memory addresses are all different.

Comparison and solution

E1 = true; makes sense through memory, after all, it's all the same memory.

E2 = false; can also be seen here if you are using a higher version of VS. Because here VS will prompt:

May be an unintended reference comparison.

Since it is a reference comparison, the memory address is different, so it must be false. But if the vs version is not high, it will be more confusing. In fact, what we are doing here is a comparison of ReferenceEquals.

E3 = true; the problem here is in the .net code. The string type Equals method is overloaded.

/ / Determines whether two strings match. Public override bool Equals ([NotNullWhen (true)] object? Obj) {if (object.ReferenceEquals (this, obj)) return true; if (! (obj is string str)) return false; if (this.Length! = str.Length) return false; return EqualsHelper (this, str);}

The EqualsHelper method is finally called as follows. (under .net 6)

/ / Optimized byte-based SequenceEquals. The "length" parameter for this one is declared a nuint rather than int as we also use it for types other than byte / / where the length can exceed 2Gb once scaled by sizeof (T) Public static unsafe bool SequenceEqual (ref byte first, ref byte second, nuint length)

Because the implementation is too complex (.net framework 4.5.2 is relatively simple, compare char directly by length, and check it out if you are interested), we will not stick to the specific implementation here. It's easy to see that the purpose of the comparison here is to compare whether two pieces of memory are equal, obviously true.

E4 = false; is here to compare the effects of different serialization methods, similar to e2, and the result is obviously false.

This is the end of the article on "Special Application scenario Analysis of string invariance and Equality judgment in .net". Thank you for reading! I believe you all have a certain understanding of "special application scenario analysis of string invariance and equality judgment in .net". If you want to learn more, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report