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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C#的方法的参数修饰符详解

發(fā)布時間:2023/11/27 生活经验 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#的方法的参数修饰符详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

方法參數(shù)修飾
(啥也沒有) 如果參數(shù)的修飾是啥也沒有,那么其參數(shù)傳遞的方式是值傳遞,接受方收到的是原始數(shù)據(jù)的拷貝
out 說明了參數(shù)是引用傳遞。
params 可變參,注意了這種修飾符針對的參數(shù)一定是最后一個參數(shù)
ref 引用傳遞,參數(shù)的內(nèi)容會改變。

// 缺省是傳值
public static int Add(int x, int y)
{
int ans = x + y;
x = 10000;
y = 88888;
return ans;
}

static void Main(string[] args)
{
int x = 9, y = 10;
Console.WriteLine("調(diào)用前: X: {0}, Y: {1}", x, y);
Console.WriteLine("結(jié)果: {0}", Add(x, y));
Console.WriteLine("調(diào)用后: X: {0}, Y: {1}", x, y);
}

// 輸出修飾
public static void Add(int x, int y, out int ans)
{
ans = x + y;
}

static void Main(string[] args)
{
// 不需要進行本地賦值
int ans;
Add(90, 90, out ans);
Console.WriteLine("90 + 90 = {0} ", ans);
}

// 多個輸出修飾
public static void FillTheseValues(out int a, out string b, out bool c)
{
a = 9;
b = "Enjoy your string.";
c = true;
}

static void Main(string[] args)
{
int i;
string str;
bool b;
FillTheseValues(out i, out str, out b);
Console.WriteLine("Int is: {0}", i);
Console.WriteLine("String is: {0}", str);
Console.WriteLine("Boolean is: {0}", b);
}

//引用修飾
public static void SwapStrings(ref string s1, ref string s2)
{
string tempStr = s1;
s1 = s2;
s2 = tempStr;
}
This method can be called as so:
static void Main(string[] args)
{
string s = "第一個字符串";
string s2 = "其他字符串";
Console.WriteLine("之前: {0}, {1} ", s, s2);
SwapStrings(ref s, ref s2);
Console.WriteLine("之后: {0}, {1} ", s, s2);
}

//可變參
static double CalculateAverage(params double[] values)
{
double sum = 0;
for (int i = 0; i < values.Length; i++)
sum += values[i];
return (sum / values.Length);
}

static void Main(string[] args)
{
// Pass in a comma-delimited list of doubles...
double average;
average = CalculateAverage(4.0, 3.2, 5.7);
Console.WriteLine("4.0, 3.2, 5.7 的平均數(shù)是: {0}",
average);
double[] data = { 4.0, 3.2, 5.7 };
average = CalculateAverage(data);
Console.WriteLine("Average of data is: {0}", average);
Console.ReadLine();
}

轉(zhuǎn)載于:https://www.cnblogs.com/ayajenson/archive/2008/08/05/1261003.html

總結(jié)

以上是生活随笔為你收集整理的C#的方法的参数修饰符详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。