自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解C#使用ref和out

開發(fā) 后端
這里介紹C#使用ref和out,包括介紹值類型變量直接包含其數(shù)據(jù),這與引用類型變量不同,后者包含對其數(shù)據(jù)的引用。

在C#中,既可以通過值也可以通過引用傳遞參數(shù)。通過引用傳遞參數(shù)允許函數(shù)成員(方法、屬性、索引器、運(yùn)算符和構(gòu)造函數(shù))更改參數(shù)的值,并保持該更改。若要通過引用傳遞參數(shù),請C#使用ref和out傳遞數(shù)組。為簡單起見,本主題的示例中只使用了ref關(guān)鍵字。有關(guān)ref和out傳遞數(shù)組之間的差異的信息,請參見、C#使用ref和out傳遞數(shù)組。

本主題包括下列章節(jié):
◆傳遞值類型參數(shù)
◆傳遞引用類型參數(shù)

值類型變量直接包含其數(shù)據(jù),這與引用類型變量不同,后者包含對其數(shù)據(jù)的引用。因此,向方法傳遞值類型變量意味著向方法傳遞變量的一個(gè)副本。方法內(nèi)發(fā)生的對參數(shù)的更改對該變量中存儲(chǔ)的原始數(shù)據(jù)無任何影響。如果希望所調(diào)用的方法更改參數(shù)值,必須使用ref或out關(guān)鍵字通過引用傳遞該參數(shù)。為了簡單起見,以下示例使用ref。

下面的示例演示通過值傳遞值類型參數(shù)。通過值將變量myInt傳遞給方法SquareIt。方法內(nèi)發(fā)生的任何更改對變量的原始值無任何影響。

  1. //PassingParams1.cs  
  2. usingSystem;  
  3. classPassingValByVal  
  4. ...{  
  5. staticvoidSquareIt(intx)  
  6. //Theparameterxispassedbyvalue.  
  7. //ChangestoxwillnotaffecttheoriginalvalueofmyInt.  
  8. ...{  
  9. x*=x;  
  10. Console.WriteLine("Thevalueinsidethemethod:{0}",x);  
  11. }  
  12. publicstaticvoidMain()  
  13. ...{  
  14. intmyInt=5;  
  15. Console.WriteLine("Thevaluebeforecallingthemethod:{0}",  
  16. myInt);  
  17. SquareIt(myInt);//PassingmyIntbyvalue.  
  18. Console.WriteLine("Thevalueaftercallingthemethod:{0}",  
  19. myInt);  
  20. }  

當(dāng)調(diào)用SquareIt時(shí),myInt的內(nèi)容被復(fù)制到參數(shù)x中,在方法內(nèi)將該參數(shù)求平方。但在Main中,myInt的值在調(diào)用SquareIt方法之前和之后是相同的。實(shí)際上,方法內(nèi)發(fā)生的更改只影響局部變量x。

下面的示例除使用ref關(guān)鍵字傳遞參數(shù)以外,其余與上面代碼相同。參數(shù)的值在調(diào)用方法后發(fā)生更改。

  1. //PassingParams2.cs  
  2. usingSystem;  
  3. classPassingValByRef  
  4. ...{  
  5. staticvoidSquareIt(refintx)  
  6. //Theparameterxispassedbyreference.  
  7. //ChangestoxwillaffecttheoriginalvalueofmyInt.  
  8. ...{  
  9. x*=x;  
  10. Console.WriteLine("Thevalueinsidethemethod:{0}",x);  
  11. }  
  12. publicstaticvoidMain()  
  13. ...{  
  14. intmyInt=5;  
  15. Console.WriteLine("Thevaluebeforecallingthemethod:{0}",  
  16. myInt);  
  17. SquareIt(refmyInt);//PassingmyIntbyreference.  
  18. Console.WriteLine("Thevalueaftercallingthemethod:{0}",  
  19. myInt);  
  20. }  

以上介紹C#使用ref和out傳遞數(shù)組

【編輯推薦】

  1. 如何用C#和ADO.NET訪問
  2. 淺析C# Switch語句
  3. C#驗(yàn)證輸入方法詳解
  4. 簡單介紹C# 匿名方法
  5. C# FileSystemWatcher對象
責(zé)任編輯:佚名 來源: 51CTO.com
相關(guān)推薦

2009-08-07 13:18:48

C#傳遞數(shù)組

2009-08-27 14:29:15

C# explicti

2009-09-24 15:20:54

C#接口定義

2025-04-22 08:16:37

refC#參數(shù)

2009-09-02 18:44:19

C#遞歸

2009-08-25 16:54:28

C# RichText

2009-09-11 11:04:23

C# WinForm自

2009-08-14 09:27:27

C#構(gòu)造函數(shù)的特性

2009-08-24 11:23:41

C# TimeLabe

2009-09-07 16:13:56

C# MessageB

2009-07-30 18:20:21

C#繼承

2009-09-01 16:07:04

C#命名規(guī)約

2009-08-06 15:40:11

C#裝箱和拆箱

2024-09-23 16:55:18

C#代碼開發(fā)

2009-08-14 17:09:48

C#引用類型

2009-08-13 15:41:50

C#結(jié)構(gòu)體指針

2009-08-13 17:04:09

C#語言C#程序

2009-08-27 16:11:03

C# delegateC# event

2010-09-14 14:05:42

C#委托

2009-08-25 18:04:30

C#實(shí)現(xiàn)Singlet
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號