日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#中Ref和out的使用区别

發布時間:2025/7/14 C# 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中Ref和out的使用区别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載:http://msdn.microsoft.com/zh-cn/vcsharp/aa336814(en-us).aspx

總結:
1. out傳參數前不必分配值,而ref必須賦值再傳遞。
2. 如果參數(out或ref)太多,建議使用Struct或Class代替。

Why does C# have both 'ref' and 'out'?

Ref and out parameter passing modes are used to allow a method to alter variables passed in by the caller. The difference between ref and out is subtle but important. Each parameter passing mode is designed to apply to a slightly different programming scenario. The important difference between out and ref parameters is the definite assignment rules used by each.

The caller of a method which takes an out parameter is not required to assign to the variable passed as the out parameter prior to the call; however, the callee is required to assign to the out parameter before returning.

Here's a simple example:

class OutExample
{
????? // Splits a string containing a first and last name separated
????? // by a space into two distinct strings, one containing the first name and one containing the last name

????? static void SplitName(string fullName, out string firstName, out string lastName)
????? {
??????????? // NOTE: firstName and lastName have not been assigned to yet. ?Their values cannot be used.
??????????? int spaceIndex = fullName.IndexOf(' ');
??????????? firstName = fullName.Substring(0, spaceIndex).Trim();
??????????? lastName = fullName.Substring(spaceIndex).Trim();
????? }

????? static void Main()
????? {
??????????? string fullName = "Yuan Sha";
??????????? string firstName;
??????????? string lastName;

? ????????? // NOTE: firstName and lastName have not been assigned yet.? Their values may not be used.
??????????? SplitName(fullName, out firstName, out lastName);
??????????? // NOTE: firstName and lastName have been assigned, because the out parameter passing mode guarantees it.

??????????? System.Console.WriteLine("First Name '{0}'. Last Name '{1}'", firstName, lastName);
????? }
}

One way to think of out parameters is that they are like additional return values of a method. They are very convenient when a method returns more than one value, in this example firstName and lastName. Out parameters can be abused however. As a matter of good programming style if you find yourself writing a method with many out parameters then you should think about refactoring your code. One possible solution is to package all the return values into a single struct.

In contrast ref parameters are considered initially assigned by the callee. As such, the callee is not required to assign to the ref parameter before use. Ref parameters are passed both into and out of a method.

Here's an example:

class RefExample
{
????? static object FindNext(object value, object[] data, ref int index)
????? {
??????????? // NOTE: index can be used here because it is a ref parameter
??????????? while (index < data.Length)
??????????? {
????????????????? if (data[index].Equals(value))
????????????????? {
??????????????????????? return data[index];
????????????????? }
?????????????????? index += 1;
??????????? }
???????????? return null;
????? }

????? static void Main()
????? {
??????????? object[] data = new object[] {1,2,3,4,2,3,4,5,3};

??????????? int index = 0;
??????????? // NOTE: must assign to index before passing it as a ref parameter
??????????? while (FindNext(3, data, ref index) != null)
??????????? {
????????????????? // NOTE: that FindNext may have modified the value of index
????????????????? System.Console.WriteLine("Found at index {0}", index);
????????????????? index += 1;
??????????? }

??????????? System.Console.WriteLine("Done Find");
????? }
}

The two parameter passing modes addressed by out and ref are subtly different, however they are both very common. The subtle difference between these modes leads to some very common programming errors. These include:

  • not assigning a value to an out parameter in all control flow paths
  • not assigning a value to variable which is used as a ref parameter

Because the C# language assigns different definite assignment rules to these different parameter passing modes, these common coding errors are caught by the compiler as being incorrect C# code.

The crux of the decision to include both ref and out parameter passing modes was that allowing the compiler to detect these common coding errors was worth the additional complexity of having both ref and out parameter passing modes in the language.

轉載于:https://www.cnblogs.com/simonhaninmelbourne/archive/2009/07/31/1535593.html

總結

以上是生活随笔為你收集整理的C#中Ref和out的使用区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。