類型 | 別名 | 允許的值 |
sbyte | System.SByte | -128~127 |
byte | System.Byte | 0~255 |
short | System.Int16 | -32768~32767 |
ushort | System.Uint16 | 0~65535 |
int | System.Int32 | -2147483648~2147483647 |
uint | System.UInt32 | 0~4294967295 |
long | System.Int64 | -9223372036854775808~9223372036854775807 |
ulong | System.UInt64 | 0~18446744073709551615 |
char | System.Char | 一個Unicode字符,0~65535 |
bool | System.Boolean | true/false |
sring | System.String | 一組字符 |
float | System.Single | 1.5*10^-45~3.4*10^38 |
double | System.Double | 5.0*10^-324~1.7*10^308 |
decimal | System.Decimal | 1.0*10^-28~7.9*10^28 |
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//【例子1】枚舉的基本應用
//1)賦值是可以使用其枚舉值賦值,如ct=emCardType.Temic;也可以直接用數值賦值,如ct = (emCardType)3;也可用將字符串經過轉換// 賦值給枚舉變量,如ct = (emCardType)Enum.Parse(typeof(emCardType), "M1")
//2)使用WritenLine時其實是調用其ToString()方法將其轉換為字符串
//3)可以將沒有枚舉到的數值賦給枚舉變量,但ToString()方法顯示的字符串為其值
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
enum emCardType:byte
{
Temic,
Em=2,
ID,
M1
}
emCardType ct;
ct=emCardType.Temic ;
Console.WriteLine("ct is {0},value is {1}", ct,(byte)ct); //顯示"ct is Temic,value is 0"
ct++;
Console.WriteLine("ct is {0},value is {1}", ct,(byte)ct); //顯示"ct is 1,value is 1"
ct = (emCardType)Enum.Parse(typeof(emCardType), "M1");
Console.WriteLine("ct is {0},value is {1}", ct, (byte)ct); //顯示"ct is M1,value is 4"
ct = (emCardType)3;
Console.WriteLine("ct is {0},value is {1}", ct.ToString(), (byte)ct); //顯示"ct is ID,value is 3"
Console.ReadKey();
1)一維數組定義
int[] myIntArray = { 5, 9, 10, 2, 99 };
int[] myIntArray = new int[5];
int[] myIntArray = new int[5] { 5, 9, 10, 2, 99 };
2)多維數組定義
double[,] hillHeight = new double[3, 4];
double[,] hillHeight = { { 1, 2, 3, 4 }, { 2, 3, 4, 5 }, { 3, 4, 5, 6 } };
3)數組的數組
int [][] myarray
myarray=new int[2][];
myarray[0]=new int[3];
myarray[1]=new int[4];
myarray=new int[3][](new int[]{1,2,3},new int[]{1},new int[]{1,2});
foreach (int[] subarray in myarray)
{
foreach(int nValue in subarray)
Console.WriteLine(nValue);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//【例子1】數組復制
//若直接使用=號則兩個數組引用相同的值,要使用CopyTo方法進行復制
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int[] myInt = new int[5]{5,2,1,8,4};
foreach (int i in myInt)
Console.Write(i + ",");
Console.WriteLine("");
int[] myInt2=new int[5];
myInt.CopyTo(myInt2,0);
//myInt2 = myInt;
myInt[1] = 3;
foreach (int i in myInt2)
Console.Write(i + ",");
【例子1】
class PRogram
{
delegate double ProcessDelegate(double param1,double param2);
static double Multiply(double param1,double param2)
{
return param1*param2;
}
static double Divide(double param1,double param2)
{
return param1/param2;
}
static void Main(string[] args)
{
ProcessDelegate process;
if (input=="M") //根據輸入的不同將其實例化為不同的函數
process=new processDelegate(Multiply);
else
process=new processDelegate(Divide);
double fRet=process(f1,f2);
}
}
【例子2】
private delegate int mif_selecom(int nCom, int nBaud);
private void button1_Click(object sender, EventArgs e)
{
IntPtr pDll = LoadLibrary("LC32RFRW.dll");
IntPtr pAddressOfFunctionToCall = GetProcAddress(pDll, "mif_selecom");
mif_selecom DoSeleCom = (mif_selecom)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall,
typeof(mif_selecom));
}
新聞熱點
疑難解答