[C # Study Notes] I. Basic knowledge,
1.1 data type (P43)
Type |
Alias |
Allowed value |
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 |
A Unicode character, ranging from 0 ~ 65535 |
Bool |
System. Boolean |
True/false |
Sring |
System. String |
A group of characters |
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.2 Enumeration type (P97)
//////////////////////////////////////// //////////////////////////////////////// ///////////////////////////////////////
// [Example 1] basic application of enumeration
// 1) You can use its enumerated values to assign values, such as ct = emCardType. temic; you can also directly assign values using numeric values, such as ct = (emCardType) 3; you can also convert the string and assign it to the enumerated variable, such as ct = (emCardType) Enum. parse (typeof (emCardType), "M1 ")
// 2) When WritenLine is used, the ToString () method is called to convert it to a string.
// 3) the enumerated value can be assigned to the enumerated variable, but the string displayed by the ToString () method is its value.
//////////////////////////////////////// //////////////////////////////////////// ///////////////////////////////////////
Enum emCardType: byte
{
Temic,
Em = 2,
ID,
M1
}
EmCardType ct;
Ct = emCardType. Temic;
Console. WriteLine ("ct is {0}, value is {1}", ct, (byte) ct); // display "ct is Temic, value is 0"
Ct ++;
Console. WriteLine ("ct is {0}, value is {1}", ct, (byte) ct); // display "ct is 1, value is 1"
Ct = (emCardType) Enum. Parse (typeof (emCardType), "M1 ");
Console. WriteLine ("ct is {0}, value is {1}", ct, (byte) ct); // display "ct is M1, value is 4"
Ct = (emCardType) 3;
Console. writeLine ("ct is {0}, value is {1}", ct. toString (), (byte) ct); // display "ct is ID, value is 3"
Console. ReadKey ();
Array 1.3
1) One-dimensional array Definition
Int [] myIntArray = {5, 9, 10, 2, 99 };
Int [] myIntArray = new int [5];
Int [] myIntArray = new int [5] {5, 9, 10, 2, 99 };
2) multi-dimensional array Definition
Double [,] hillHeight = new double [3, 4];
Double [,] hillHeight = {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6 }};
3) array Array
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 );
}
//////////////////////////////////////// //////////////////////////////////////// ///////////////////////////////////////
// [Example 1] copying Arrays
// If the = sign is used directly, the two arrays reference the same value. You must use the CopyTo Method for copying.
//////////////////////////////////////// //////////////////////////////////////// ///////////////////////////////////////
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.4 Delegation)
[Example 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") // instantiate it as a different function based on the input.
Process = new processDelegate (Multiply );
Else
Process = new processDelegate (Divide );
Double fRet = process (f1, f2 );
}
}
[Example 2]]
Private delegate int mif_selecom (int nCom, int nBaud );
Private void button#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 ));
}