In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "various situations and result analysis of bachela in C#". In daily operation, I believe that many people have doubts about the various situations and result analysis of byoga in C#. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "multiple situations and result analysis of byoga in C#"! Next, please follow the editor to study!
Two variables of the same type, an and b, and have the following relationship:
Bachela
If a changes, will b also change?
The situation is very complicated and can be discussed in the following ways:
1) single entity
1. Simple type
Let's take a look at int:
Inta=1; intb=a; axi2; Console.WriteLine ("b:" + b)
Output result:
Take another look at string:
Stringa= "1"; stringb=a; a = "2"; Console.WriteLine ("b:" + b)
Output result:
If you are worried, you can also test Enum, and the results are similar, as detailed in Demo.
Conclusion: a simple type is the most basic unit of a compound type, which is atomic and cannot be split again, so neither the value type double, int nor the reference type string,b changes with a, because they point to the same address on the global stack (the managed heap for string).
two。 Compound type
Compound types are made up of simple types such as string, int, and double.
Define a composite reference type (class) and a composite value type (struct), respectively.
ClassUserInfo {publicstringUserName; publicintAge;} structUserInfo2 {publicstringUserName; publicintAge;}
Let's first discuss the reference types:
UserInfoa=newUserInfo () {UserName= "Baobao", Age=27}; UserInfob=a; a.UserName = "AndersLiu"; a.Agett30; Console.WriteLine ("b.UserName:" + b.UserName); Console.WriteLine ("b.Age:" + b.Age)
Output result:
Conclusion: B and a still point to the address of the same UserInfo instance on the managed heap. The members of the UserInfo instance contain the addresses of UserName and Age on the managed heap and the global stack, respectively. So modifying the members of a, UserName and Age, only changes the addresses of these two members, not the address of the UserInfo instance, so the members of b, UserName and Age, will also change.
Let's partially modify the above code:
UserInfoa=newUserInfo () {UserName= "Baobao", Age=27}; UserInfob=a; / a.UserName = "AndersLiu"; / / a.UserName30; a=newUserInfo () {UserName= "AndersLiu", Age=30}; Console.WriteLine ("b.UserName:" + b.UserName); Console.WriteLine ("b.Age:" + b.Age)
Output result:
Conclusion: re-instantiate a, causing it to point to the address of a new UserInfo instance. However, b still points to the address of the original UserInfo instance, so b does not change as a changes. From then on, b and an are two variables without any relationship.
Let's take a look at the value type again:
UserInfo2a=newUserInfo2 () {UserName= "Baobao", Age=27}; UserInfo2b=a; a.UserName = "AndersLiu"; a.Agett30; Console.WriteLine ("b.UserName:" + b.UserName); Console.WriteLine ("b.Age:" + b.Age)
Output result:
Conclusion: the question focuses on the sentence "bachela". At this point, b points to a copy of a, pointing to a different address on the global stack. So there is no relationship between b and a, b does not change with a.
2) set
1. A modification of the addition or deletion of data in the collection.
Lista=newList (); Listb=a; a.Add (newUserInfo () {UserName= "Baobao", Age=27}); Console.WriteLine ("b.Countafteradding:" + b.Count); Console.WriteLine (); Console.WriteLine ("Aftermodifyinga [0]"); a [0] .username = "AndersLiu"; a [0] .Age = 30; Console.WriteLine ("b [0] .UserName:" + b [0] .UserName); Console.WriteLine ("b [0] .Age:" + b [0] .Age); Console.WriteLine (); a.Remove (a [0]) Console.WriteLine ("b.Countafterdeleting:" + b.Count)
Output result:
Conclusion: B changes with the increase or decrease of data in a. Because b and a point to the memory address of the same List instance on the managed heap, this is the same as the composite type.
The array can be regarded as a collection of multiple variables, so it can be treated as a set.
At this point, the study of "multiple situations and result Analysis of bachela in C#" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.