In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the C# parameters". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the C# parameters"?
Simple types (such as int,double,char), enum types, and struct types are all value types.
Class types, interface types, delegate types, and array types are all reference types.
1. Value parameter (Value parameters)
By default, parameters are value parameters.
Void Foo (StringBuilder x) {x = null;}... StringBuilder y = new StringBuilder (); y.Append ("hello"); Foo (y); Console.WriteLine (y==null)
The output is False. Because it is in the form of value transfer, although x is set to null, the value of y does not change. It is important to note, however, that reference type variables hold references, and if two reference type variables refer to the same object, modifications to the object are bound to affect the two reference type variables, as follows:
Void Foo (StringBuilder x) {x.Append ("world");}. StringBuilder y = new StringBuilder (); y.Append ("hello"); Foo (y); Console.WriteLine (y)
The output is hello world. Because after calling the Foo method, the content of the StringBuilder object referenced by y is changed to "hello world", which is achieved by calling the Append method to add "world" through the x reference in the Foo method.
2. Reference parameters (Reference parameters)
The reference parameter does not pass the value of the variable in the function member call, but the variable itself. This means that it does not allocate new memory space for variables in function member declarations, but uses the same storage space as arguments, so the values of arguments and parameters are the same at all times. To use reference parameters in C #, you must explicitly use the keyword ref in both function declarations and function calls, so that it is clear to the code that reference parameters are used.
Void Foo (ref StringBuilder x) {x = null;}. StringBuilder y = new StringBuilder (); y.Append ("hello"); Foo (ref y); Console.WriteLine (y==null)
The output is True. The reference parameter is used here, passing a reference to y (reference), not the value in y (object reference). So the change in the value of parameter x is immediately reflected in y. After the Foo method is called, the value of y is also null.
Thank you for your reading, the above is the content of "what are the C# parameters". After the study of this article, I believe you have a deeper understanding of what the C# parameters are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.