通常一個方法只能返回一個值,但是如果在某些時候,我們想要返回多個值,例如某個方法將一個浮點數分割成一個整數和一個小數返回去。這個時候我們就要用到out關鍵字。
如果用ref也可以解決,但是用ref需要在初始化的時候虛設一個值,並且還要給虛設值賦初始值。
復習輸出值的格式初始化,復習了@的一個用法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace cxx
{
class Testout
{
public int getParts(double n, out double frac)
{
int whole;
whole = (int)n;
frac = n - whole; //pass fractional小數 part back through frac
return whole; //return integer portion 返回整數部分
}
}
class UseOut
{
static void Main()
{
Testout Tout = new Testout();
int i;
double f;
i = Tout.getParts(1234.05067891023343, out f);
Console.WriteLine("整數部分:" + i);
Console.WriteLine("小數部分:{0:#.###}" , f);
Console.WriteLine("小數部分:" + f);
Console.WriteLine(@"my name is shoneworn.
welcome to my blog: www.cnblogs.com/shoneworn.
注意看@的用法,是按照自己排版輸出的。");
Console.ReadKey();
}
}
}

不積跬步,無以至千里。
轉載於:https://www.cnblogs.com/shoneworn/p/3387455.html
