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

Analysis of different points of C# parameters with examples

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

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "Analysis of different points of C# parameters". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Ref is the address where the parameter is passed, and out is the return value. There are some similarities between the two, but there are also differences in C# parameters. Variables must be assigned values before using ref, not out. The function of out will empty the variable, even if the variable has been assigned. When you exit the function, all variables referenced by out must be assigned, and those referenced by ref can be modified or not.

For the differences in C# parameters, see the following code:

Using System

Class TestApp

{

Static void outTest (out int x, out int y)

{/ / before leaving this function, you must assign values to x and y, otherwise an error will be reported.

/ / y = x

/ / the above line will report an error, because x and y are cleared after using out

It needs to be reassigned, even if it has been assigned before calling the function.

X = 1

Y = 2

}

Static void refTest (ref int x, ref int y)

{

X = 1

Y = x

}

Public static void Main ()

{

/ / out test

Int a,b

/ / variables can not be assigned before out is used.

OutTest (out a, out b)

Console.WriteLine ("a = {0}; b = {1}", aPerry b)

Int canals 11 and dudes 22

OutTest (out c, out d)

Console.WriteLine ("c = {0}; d = {1}", cPerry d)

/ / ref test

Int m,n

/ / refTest (ref m, ref n)

/ / the above line will make an error. Variables must be assigned before ref is used.

Int oysters 11 and 22

RefTest (ref o, ref p)

Console.WriteLine ("o = {0}; p = {1}", o < p)

}

}

This is the end of the content of "example Analysis of differences in C# parameters". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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