- C# 7 and .NET Core Cookbook
- Dirk Strauss
- 212字
- 2021-07-03 00:11:55
How to do it...
- Inside the Chapter1 class, create a new method called GetLargest(). The method is nothing special. It only gets the largest of two values and returns it to the calling code.
public int GetLargest(int valueA, int valueB)
{
if (valueA > valueB)
return valueA;
else
return valueB;
}
- Create a second method with the same name. Only this time, add the ref keyword.
public ref int GetLargest(ref int valueA, ref int valueB)
{
if (valueA > valueB)
return ref valueA;
else
return ref valueB;
}
- In the static void Main method, create an instance to the Chapter1 class and call the GetLargest() method. Increment the variable val and write the variable values to the console window.
int a = 10;
int b = 20;
Chapter1 ch1 = new Chapter1();
int val = ch1.GetLargest(a, b);
val += 25;
WriteLine($"val = {val} a = {a} b = ");
- Then, write the following code just after the previous calling code, but call the ref ch1.GetLargest() method. Increment the refVal variable and write the variable values to the console window.
ref int refVal = ref ch1.GetLargest(ref a, ref b);
refVal += 25;
WriteLine($"refVal = {refVal} a = {a} b = ");
- Run your console application and consider the output displayed.

推薦閱讀
- 測試驅動開發(fā):入門、實戰(zhàn)與進階
- 青少年軟件編程基礎與實戰(zhàn)(圖形化編程三級)
- 認識編程:以Python語言講透編程的本質
- BeagleBone Media Center
- React.js Essentials
- PLC編程及應用實戰(zhàn)
- Spring Boot Cookbook
- ElasticSearch Cookbook(Second Edition)
- C語言程序設計實訓教程與水平考試指導
- Data Science Algorithms in a Week
- ASP.NET 4.0 Web程序設計
- Mobile Forensics:Advanced Investigative Strategies
- Android嵌入式系統(tǒng)程序開發(fā)(基于Cortex-A8)
- Python面向對象編程(第4版)
- 微信公眾平臺服務號開發(fā):揭秘九大高級接口