C#重點(diǎn)知識(shí)詳解(一)
2024-07-21 02:20:00
供稿:網(wǎng)友
前沿
在微軟的.net推出后,關(guān)于c#的有關(guān)文章也相繼出現(xiàn),作為微軟的重要的與java抗衡的語(yǔ)言,c#具有很多優(yōu)點(diǎn)。本文將選一些c#語(yǔ)言中的重要知識(shí)詳細(xì)介紹,
第一章:參數(shù)
1。1 in 參數(shù)
c#種的四種參數(shù)形式:
一般參數(shù)
in參數(shù)
out參數(shù)
參數(shù)數(shù)列
本章將介紹后三種的使用。
在c語(yǔ)言你可以通傳遞地址(即實(shí)參)或是delphi語(yǔ)言中通過(guò)var指示符傳遞地址參數(shù)來(lái)進(jìn)行數(shù)據(jù)排序等操作,在c#語(yǔ)言中,是如何做的呢?"in"關(guān)鍵字可以幫助你。這個(gè)關(guān)鍵字可以通過(guò)參數(shù)傳遞你想返回的值。
namespace testrefp
{
using system;
public class myclass
{
public static void reftest(ref int ival1 )
{
ival1 += 2;
}
public static void main()
{
int i=3; //變量需要初始化
reftest(ref i );
console.writeline(i);
}
}
}
必須注意的是變量要須先初始化。
結(jié)果:
5
1。2 out 參數(shù)
你是否想一次返回多個(gè)值?在c++語(yǔ)言中這項(xiàng)任務(wù)基本上是不可能完成的任務(wù)。在c#中"out"關(guān)鍵字可以幫助你輕松完成。這個(gè)關(guān)鍵字可以通過(guò)參數(shù)一次返回多個(gè)值。
public class mathclass
{
public static int testout(out int ival1, out int ival2)
{
ival1 = 10;
ival2 = 20;
return 0;
}
public static void main()
{
int i, j; // 變量不需要初始化。
console.writeline(testout(out i, out j));
console.writeline(i);
console.writeline(j);
}
}
結(jié)果:
0 10 20
1。3 參數(shù)數(shù)列
參數(shù)數(shù)列能夠使多個(gè)相關(guān)的參數(shù)被單個(gè)數(shù)列代表,換就話(huà)說(shuō),參數(shù)數(shù)列就是變量的長(zhǎng)度。
using system;
class test
{
static void f(params int[] args) {
console.writeline("# 參數(shù): {0}", args.length);
for (int i = 0; i < args.length; i++)
console.writeline("/targs[{0}] = {1}", i, args[i]);
}
static void main() {
f();
f(1);
f(1, 2);
f(1, 2, 3);
f(new int[] {1, 2, 3, 4});
}
}
以下為輸出結(jié)果:
# 參數(shù): 0
# 參數(shù): 1
args[0] = 1
# 參數(shù): 2
args[0] = 1
args[1] = 2
# 參數(shù): 3
args[0] = 1
args[1] = 2
args[2] = 3
# 參數(shù): 4
args[0] = 1
args[1] = 2
args[2] = 3
args[3]
本文來(lái)源于網(wǎng)頁(yè)設(shè)計(jì)愛(ài)好者web開(kāi)發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問(wèn)。