官术网_书友最值得收藏!

How to do it...

  1. 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;
}
  1. 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;
}
  1. 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 = {b} ");
  1. 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 = {b} ");
  1. Run your console application and consider the output displayed.
主站蜘蛛池模板: 垣曲县| 天水市| 乌兰察布市| 石家庄市| 靖江市| 兴文县| 静乐县| 延安市| 河东区| 长葛市| 藁城市| 扎赉特旗| 鄂州市| 福泉市| 赤水市| 疏附县| 余干县| 清丰县| 彭阳县| 清河县| 哈尔滨市| 华宁县| 札达县| 阿瓦提县| 伊通| 岐山县| 敦煌市| 尼玛县| 新余市| 介休市| 麦盖提县| 广河县| 浮梁县| 易门县| 九江县| 广灵县| 荃湾区| 郑州市| 扎鲁特旗| 永福县| 东光县|