C#重點知識詳解(一)(轉)
2024-07-21 02:22:13
供稿:網友
在微軟的.net推出后,關于c#的有關文章也相繼出現,作為微軟的重要的與java抗衡的語言,c#具有很多優點。本文將選一些c#語言中的重要知識詳細介紹,
第一章:參數
1。1 in 參數
c#種的四種參數形式:
一般參數
in參數
out參數
參數數列
本章將介紹后三種的使用。
在c語言你可以通傳遞地址(即實參)或是delphi語言中通過var指示符傳遞地址參數來進行數據排序等操作,在c#語言中,是如何做的呢?"in"關鍵字可以幫助你。這個關鍵字可以通過參數傳遞你想返回的值。
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);
}
}
}
必須注意的是變量要須先初始化。
結果:
5
1。2 out 參數
你是否想一次返回多個值?在c++語言中這項任務基本上是不可能完成的任務。在c#中"out"關鍵字可以幫助你輕松完成。這個關鍵字可以通過參數一次返回多個值。
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);
}
}
結果:
0 10 20
1。3 參數數列
參數數列能夠使多個相關的參數被單個數列代表,換就話說,參數數列就是變量的長度。
using system;
class test
{
static void f(params int[] args) {
console.writeline("# 參數: {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});
}
}
以下為輸出結果:
# 參數: 0
# 參數: 1
args[0] = 1
# 參數: 2
args[0] = 1
args[1] = 2
# 參數: 3
args[0] = 1
args[1] = 2
args[2] = 3
# 參數: 4
args[0] = 1
args[1] = 2
args[2] = 3
args[3]